获取是否选中了 Groupbox 中的任何 RadioButton
Get if any RadioButton in a Groupbox was checked
我有一个 Windows 表单应用程序,其中我有几个单选按钮存储在 GroupBox
中。我需要根据所选的单选按钮启用不同的 GroupBox
。
groubBox.Enter
似乎不是我要找的 EventHandler
。有什么办法可以按照我的方式进行,还是我必须为每个 radiobutton.CheckedChanged
?
创建一个处理程序
编辑
应用程序的工作流程是:
一个文件被选中 → GroupBox
被启用 → Panel
/ComboBox
, TextBox
根据所选 RadioButton
让 gbRadioButtons
成为 GroupBox 的名称,然后您可以遍历该特定 Groupbox 中的每个单选按钮,并使用以下代码检查它是否被选中(包括您想要的代码)检查):
bool isAnyRadioButtonChecked = false;
foreach (RadioButton rdo in gbRadioButtons.Controls.OfType<RadioButton>())
{
if (rdo.Checked)
{
isAnyRadioButtonChecked=true;
break;
}
}
if (isAnyRadioButtonChecked)
{
// Code here one button is checked
}
else
{
// Print message no button is selected
}
创建 CheckedChanged
事件处理程序,一个用于所有单选按钮
设置 RadioButton.Tag
以引用 GroupBox
它的响应者
例如在构造函数中
public YourForm()
{
radioButton1.Tag = groupBox1;
radioButton2.Tag = groupBox2;
radioButton3.Tag = groupBox3;
radioButton1.CheckedChanged += radioButtons_CheckedChanged;
radioButton2.CheckedChanged += radioButtons_CheckedChanged;
radioButton3.CheckedChanged += radioButtons_CheckedChanged;
}
void radioButtons_CheckedChanged(object sender, EventArgs e)
{
RadioButton button = sender as RadioButton;
if (button == null) return;
GroupBox box = button.Tag as GroupBox
if (box == null) return;
box.Enabled = button.Checked;
}
启用GroupBox
将启用其中的所有子控件
我有一个 Windows 表单应用程序,其中我有几个单选按钮存储在 GroupBox
中。我需要根据所选的单选按钮启用不同的 GroupBox
。
groubBox.Enter
似乎不是我要找的 EventHandler
。有什么办法可以按照我的方式进行,还是我必须为每个 radiobutton.CheckedChanged
?
编辑
应用程序的工作流程是:
一个文件被选中 → GroupBox
被启用 → Panel
/ComboBox
, TextBox
根据所选 RadioButton
让 gbRadioButtons
成为 GroupBox 的名称,然后您可以遍历该特定 Groupbox 中的每个单选按钮,并使用以下代码检查它是否被选中(包括您想要的代码)检查):
bool isAnyRadioButtonChecked = false;
foreach (RadioButton rdo in gbRadioButtons.Controls.OfType<RadioButton>())
{
if (rdo.Checked)
{
isAnyRadioButtonChecked=true;
break;
}
}
if (isAnyRadioButtonChecked)
{
// Code here one button is checked
}
else
{
// Print message no button is selected
}
创建 CheckedChanged
事件处理程序,一个用于所有单选按钮
设置 RadioButton.Tag
以引用 GroupBox
它的响应者
例如在构造函数中
public YourForm()
{
radioButton1.Tag = groupBox1;
radioButton2.Tag = groupBox2;
radioButton3.Tag = groupBox3;
radioButton1.CheckedChanged += radioButtons_CheckedChanged;
radioButton2.CheckedChanged += radioButtons_CheckedChanged;
radioButton3.CheckedChanged += radioButtons_CheckedChanged;
}
void radioButtons_CheckedChanged(object sender, EventArgs e)
{
RadioButton button = sender as RadioButton;
if (button == null) return;
GroupBox box = button.Tag as GroupBox
if (box == null) return;
box.Enabled = button.Checked;
}
启用GroupBox
将启用其中的所有子控件