foreach (Control c in this.Controls) { if (c is System.Windows.Forms.RadioButton) { if (((RadioButton)c).Checked) { MessageBox.Show(c.Name+"被选中"); } } }
以上方法,循环遍历窗体内所有的控件,再进行判断看是否为RadioButton控件,最后判断是否被中.不过少加了句代码.用下面这个: foreach (Control c in this.Controls) { if (c is System.Windows.Forms.RadioButton) { if (((RadioButton)c).Checked) { MessageBox.Show(c.Name + "被选中"); break; } } }
用foreach循环一下Panel的子控件,如果是RadioButton,看看它是不是选中的就行了foreach (Control ctrl in panel.Controls) { if (ctrl is RadioButton) { RadioButton rb = ctrl as RadioButton; if (rb.选中) { return rb; } } }如果所有的RadioButton选中事件都由一个方法来处理的,也可以把sender参数记下来,它就是选中的。
For Each s As RadioButton In Controls If s.Checked Then ‘..... End If Next
遍历,再判断是不是被选中... foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Forms.RadioButton) { System.Windows.Forms.RadioButton rb = (System.Windows.Forms.RadioButton)control; if (rb.Checked == true) { MessageBox.Show(rb.Name.ToString()+" has been checked."); } } }
{
if (c is System.Windows.Forms.RadioButton)
{
if (((RadioButton)c).Checked)
{
MessageBox.Show(c.Name+"被选中");
}
}
}
{
if (c is System.Windows.Forms.RadioButton)
{
if (((RadioButton)c).Checked)
{
MessageBox.Show(c.Name + "被选中");
break;
}
}
}
{
if (ctrl is RadioButton)
{
RadioButton rb = ctrl as RadioButton;
if (rb.选中)
{
return rb;
}
}
}如果所有的RadioButton选中事件都由一个方法来处理的,也可以把sender参数记下来,它就是选中的。
If s.Checked Then
‘.....
End If
Next
foreach (System.Windows.Forms.Control control in this.Controls)
{
if (control is System.Windows.Forms.RadioButton)
{
System.Windows.Forms.RadioButton rb = (System.Windows.Forms.RadioButton)control;
if (rb.Checked == true)
{
MessageBox.Show(rb.Name.ToString()+" has been checked.");
}
}
}