简体   繁体   中英

Prevent radiobutton click

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

        // some function
        GroupBox g = createGBox();
        this.Controls.Add(g);
        g.Controls.Add(radioButton1);
        g.Controls.Add(radioButton2);
    }

    public GroupBox createGBox()
    {
        GroupBox gBox = new GroupBox();
        gBox.Location = new System.Drawing.Point(72, 105);
        gBox.Name = "BOX";
        gBox.Size = new System.Drawing.Size(200, 100);
        gBox.Text = "This is a group box";
        return gBox;
    }

Put all radiobuttons into the same GroupBox control, which you can create at runtime too. In this case the expected behaviour should be handled by control itself, without need of coding.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM