我做了一个windows程序,里面有一个panel控件,里面有若干个groupbox控件(好像是多达60个),在每个groupbox中又有四个radiobutton控件。
请问我要怎么遍历控件来获取每个groupbox控件中选中的radiobutton控件的text值呢?
请教各位大侠了,小弟感激不尽!!!!
请问我要怎么遍历控件来获取每个groupbox控件中选中的radiobutton控件的text值呢?
请教各位大侠了,小弟感激不尽!!!!
foreach (Control c in panel1.Controls)
{
if (c is GroupBox)
{
foreach (Control r in c.Controls)
{
if (r is RadioButton && ((RadioButton)r).Checked)
{
//do yoursefl
}
}
}
}
{
RadioButton rdo = sender as RadioButton;
if (rdo.Checked == true)
{
MessageBox.Show(rdo.Text);
}
}
(我已近查看过了,并不是按name属性来遍历的)
List<string> list = new List<string>();//利用foreach遍历循环panel控件中的控件Contrl
foreach(Control c in this.panel1.Control)
{
//利用is操作符,判断是否为GroupBox,若是再循环
if(c is GroupBox)
{
foreach(Control cc in c)
{
if(cc is RadioButton)
{
//利用as操作符转换为原本类型,取其Text属性。
list.Add((cc as RadioButton).Text);
}
}
}
}
//创建List<T>集合接受每个RadioButton的Text值
List<string> list = new List<string>();//利用foreach遍历循环panel控件中的控件Contrl
foreach(Control c in this.panel1.Control)
{
//利用is操作符,判断是否为GroupBox,若是再循环
if(c is GroupBox)
{
foreach(Control cc in c)
{
if(cc is RadioButton)
{
//判断有没有选中
if((cc as RadioButton).Checked)
{
//利用as操作符转换为原本类型,取其Text属性。
list.Add((cc as RadioButton).Text);
}
}
}
}
}