這個問題也很多人在找,真麻煩,我也找了好幾小時,更扯的是即使是物件順序調整了,選取值仍然顛倒,比如說:「有2個Radiobutton ,放在一起,左邊一個,右邊一個,我們希望按左邊是0,按右邊傳1(即依次序)。」可是,事實就剛好相反,它不理你。
想到最後,突然發現,即然是要依順序,那就取Tabindex即可,所以,我在Form底下設定了一個GroupBox,在裡面放了兩個Radiobutton。加了這樣的程式碼在GroupBox的裡面,若有需要的人可以參考看看。
public int Getval()
{
int cnt = this.Controls.Count;
int i = 0;
string nstr = "";
RadioButton radioBtn = null;
for (i = 0; i < cnt; i++)
{
Control ctl = this.Controls[i];
nstr = ctl.GetType().ToString();
nstr = nstr.ToUpper(System.Globalization.CultureInfo.CurrentCulture);
if (nstr.IndexOf("RADIO") > -1)
{
radioBtn=(RadioButton)ctl;
if (radioBtn.Checked)
{
value = radioBtn.TabIndex ;//這就是答案了!
return value; //value是自訂屬性,在這邊是看不到初始化之處,
}
}
}
return -1; //未選
}
//如果本文有幫到你的話,請不要吝嗇幫忙點一下旁邊或是上面的廣告,贊助一下個人,非常感謝您。