multiple checkboxes allow one check at a time

2 posts, 0 answers
  1. Scott
    Scott avatar
    7 posts
    Member since:
    Feb 2011

    Posted 27 Apr 2011 Link to this post


    private
    void rchk3Yes_Click(object sender, EventArgs e)
          {
              if (((Telerik.WinControls.UI.RadCheckBox)sender).Checked == true)
              {
                  //already checked, it will uncheck
                  rchk3Yes.Checked = false;
              }
              else
              {
                  //checks itself and unchecks other checkboxes
                  if (rchkUnknown.Checked == true)
                  {
                      rchkUnknown.Checked = false;
                  }
                  if (rchk3No.Checked == true)
                  {
                      rchk3No.Checked = false;
                  }
              }
          }

    Hi I am trying to make 3 checkboxes that only allow one or none to be checked, if one is checked the others are unchecked. Here is the code I have but it doesnt work right.
     
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 29 Apr 2011 Link to this post

    Hello Scott,

    Please take a look at the following example:

    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private FlowLayoutPanel flowPanel;
        private bool localChange;
     
        public Form1()
        {
            InitializeComponent();
            flowPanel = new FlowLayoutPanel();
            flowPanel.Dock = DockStyle.Fill;
            this.Controls.Add(flowPanel);
     
            for (int i = 0; i < 10; i++)
            {
                var checkBox = new RadCheckBox();
                checkBox.ToggleStateChanged += new StateChangedEventHandler(radCheckBox_ToggleStateChanged);
                checkBox.Text = "CheckBox" + i;
                flowPanel.Controls.Add(checkBox);
            }
        }
     
        void radCheckBox_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (localChange)
            {
                return;
            }
     
            ToggleState(sender as RadCheckBox);
        }
     
        private void ToggleState(RadCheckBox radCheckBox)
        {
            localChange = true;
     
            foreach (var control in flowPanel.Controls)
            {
                var checkBox = control as RadCheckBox;
                if (checkBox != null && checkBox != radCheckBox && checkBox.ToggleState != Telerik.WinControls.Enumerations.ToggleState.Off)
                {
                    checkBox.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;
                }
            }
     
            localChange = false;
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top