日期:2014-05-19  浏览次数:20917 次

循环生成控件,如何循环取值?
请各位高手帮忙看下,我是这样循环定义的控件,如何才能获取到每个容器中选择的是哪个单选按钮呢?
Label[]   MyLabel   =   new   Label[Rcount];
Panel[]   MyPanel   =   new   Panel[Rcount];

for   (int   i   =   0;   i   <   Dt.Rows.Count;   i++)
      {
          MyLabel[i]   =   new   Label();
          MyLabel[i].Text   =   Dt.Rows[i].ItemArray[0].ToString();

          RadioButton[]   MyRadioButton   =   new   RadioButton[4];
          MyRadioButton[0]   =   new   RadioButton();
          MyRadioButton[1]   =   new   RadioButton();
          MyRadioButton[2]   =   new   RadioButton();
          MyRadioButton[3]   =   new   RadioButton();

          MyPanel[i]   =   new   Panel();
          this.Controls.Add(MyPanel[i]);
          MyPanel[i].Controls.Add(MyRadioButton[0]);
          MyPanel[i].Controls.Add(MyRadioButton[1]);
          MyPanel[i].Controls.Add(MyRadioButton[2]);
          MyPanel[i].Controls.Add(MyRadioButton[3]);
          MyPanel[i].Controls.Add(MyLabel[i]);
      }

------解决方案--------------------
使用控件的Tag标注,事件来的时候,你检查一下Tag是什么,就可以确认了

------解决方案--------------------
for (int i = 0; i < Dt.Rows.Count; i++)
{//循环每个PANEL
for(int j=0;j <4;j++)
{//循环查看每个RADIOBUTTON
MyPanel[i].MyRadioButton[j].checked==true;
MyLabel[i].text=j;//标签显示选中的序号或者内容
MyLabel[i].text=MyRadioButton[j].text;

}

}

------解决方案--------------------
在RadioButton的事件中记录选择
private void MyRadioButton_Click(object sender, EventArgs e)
{
RadioButton SelRB = (RadioButton)sender;
SelRB.Parent.Tag = SelRB;
}
取的时候
RadioButton SelRB = (RadioButton)MyPanel[i].Tag;

------解决方案--------------------
最笨的方法 最省心的方法
foreach (Control thePanel in this.Controls)
{
if (thePanel.GetType() == typeof(System.Windows.Forms.Panel))
{
foreach (Control theRiadobut in thePanel)
{
if (theRiadobut.GetType() == typeof(System.Windows.Forms.RadioButton))
{
if (((System.Windows.Forms.RadioButton)theRiadobut).Checked)
{
//此theRiadobut被选中
break;
}
}
}
}
}