I have a win form, on which there are several radionbuttons and labels and some other controls wich I am generating at run time. Not what I want when I check a radiobutton, all the radionbutton should be unchecked except the one I checked. This applies to every radiobutton. In short, I want one radionbutton checked at a time.
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;
}
}
}
}
Here is the code above. The problem with this code is that, when I check a radiobutton, if there is any other radiobutton that is checked, and I try to uncheck it, its checkedchanged event is also fired, that causes all the radiobutton unchecked again. I hope I am clear what I want to convey.
Please provide some solution.
Thanks
Have you tried using a groupbox for all of the radiobuttons? This is the default function you are asking for.
EDIT : to clarify your questions