C#:如何检查表单上的所有单选按钮
本文关键字:表单 单选按钮 检查 何检查 | 更新日期: 2023-09-27 18:30:19
我想检查表单上的所有单选按钮,并在标签上打印所选按钮的文本。我对 C# 很陌生,我下面的方法不起作用。
public void button1_Click(object sender, EventArgs e)
{
string value;
bool isChecked = radioButton1.Checked;
if (isChecked)
{
value = radioButton1.Text;
}
else if (isChecked)
{
value = radioButton2.Text;
}
else if (isChecked)
{
value = radioButton3.Text;
}
else if (isChecked)
{
value = radioButton4.Text;
}
else
{
value = radioButton5.Text;
}
label2.Text = "Installation location:'" + value;
}
提前感谢!
获取所有RadioButtons
并遍历列表以获取Checked
列表:
foreach (RadioButton rBtn in this.Controls.OfType<RadioButton>())
{
if(rBtn.Checked)
{
label2.Text = "Installation location:'" + rBtn.Text;
break;
}
}