創建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
將使所有子控件在它
您可以創建一個所有單選按鈕使用的處理程序。當然,您必須區分此處理程序中的不同單選按鈕。這會以任何方式幫助你嗎? – Lundsern
要檢查是否有任何選中的單選按鈕,最簡單的選項是使用linq查詢。但是,對於基於選中的單選按鈕啓用或禁用組框的主要要求,最好的解決方案是使用數據綁定。 –
也請不要將解決方案作爲問題的一部分發布。這會讓未來的讀者感到困惑。你可以接受最好的解決方案,如果你認爲自己有更好的答案,你可以提出有用的答案,併發布自己的答案。也鏈接到另一個有用的答案,只需使用評論:) –