我有一個勝利的形式,其上有幾個radionbuttons和標籤和一些其他控件,我在運行時生成。當我檢查一個單選按鈕時,不是我想要的,除了我檢查的那個之外,所有的radionbutton都不應該被選中。這適用於每個單選按鈕。簡而言之,我想每次檢查一個radionbutton。防止單選按鈕
private RadioButton GenerateRadioButton(string id)
{
RadioButton _radioButton = new RadioButton();
_radioButton.Location = new Point(32, 20);
_radioButton.Margin = new Padding(4, 4, 4, 4);
_radioButton.Size = new Size(130, 36);
_radioButton.Name = id;
_radioButton.AutoSize = true;
_radioButton.Font = new Font("Arial", 16, FontStyle.Bold);
_radioButton.CheckedChanged += new System.EventHandler(RadioButton_CheckedChanged);
return _radioButton;
}
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
HandleRadioButtinClick(((RadioButton)sender).Name);
((RadioButton)sender).Checked = true;
}
private void HandleRadioButtinClick(string ctrlId)
{
FrmSpace objFrmSpace = new FrmSpace();
foreach (Control ctrl in pictureBox1.Controls)
{
if (ctrl is Panel)
{
foreach (Control ctl in ctrl.Controls)
{
if (ctl is RadioButton && ctl.Name != ctrlId)
((RadioButton)ctl).Checked = false;
}
}
}
}
這是上面的代碼。這個代碼的問題是,當我檢查單選按鈕時,如果有其他單選按鈕被選中,並且我試圖取消選中它,checkedchanged事件也會被觸發,導致所有的單選按鈕再次被取消選中。我希望我清楚我想傳達的是什麼。
請提供一些解決方案。
感謝
爲什麼你手動取消選中單選按鈕?你爲什麼不把它們分組來處理這部分。 – 2012-02-10 06:45:00
我很樂意這樣做。但是請問,如何在運行時產生它們的同時對它們進行分組? – 2012-02-10 06:46:23
將它們添加到表單中時,將它們添加到組框中而不是表單。 – 2012-02-10 06:50:00