用foreach 遍历你窗体的的所有RadioButton控件
//第一种情况:RadioButton在窗体上
foreach (Control c in this.Controls)
{
RadioButton btn = c as RadioButton;
if (btn != null)
{
if (btn.Text == "确定")
{
//进行赋值 a="a";
}
//第二种情况:RadioButton在panel等容器里面
Panel p = c as Panel;
if (p != null)
{
foreach (Control cc in p.Controls)
{
RadioButton btn = c as RadioButton;
if (c_btn != null)
{
if (c_btn.Text == "确定")
{
//进行赋值 a="a";
}
}
}
}
}
首先将这些RadioButton全部放到一个Panel中去,然后将每个RadioButton的Tag属性分别设置为a、b、c……。
string a = string.Empty;
foreach (Control c in this.panel1.Controls)
{
RadioButton rb = (RadioButton)c;
if (rb.Checked)
{
a = rb.Tag.ToString();
}
}
string a=radioButton1.checked?"a":"b";