我有一个 win 表单,上面有几个单选按钮和标签以及我在运行时生成的一些其他控件。当我检查一个单选按钮时,这不是我想要的,除了我检查的那个之外,所有的单选按钮都应该被取消选中。这适用于每个单选按钮。简而言之,我希望一次检查一个单选按钮。
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 事件也会被触发,这会导致所有单选按钮再次被取消选中。我希望我清楚我想要传达的内容。
请提供一些解决方案。
谢谢