我有一个胜利的形式,其上有几个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