multiple checkboxes allow one check at a time

5 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. aditya
    aditya avatar
    1 posts
    Member since:
    Oct 2018

    Posted 20 Oct 2018 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;
                  }
              }
          }

    www.welookups.com

  4. Uwe
    Uwe avatar
    3 posts
    Member since:
    Mar 2020

    Posted 04 Apr Link to this post

    Is there anything like a OptionSet Box?
  5. Nadya
    Admin
    Nadya avatar
    244 posts

    Posted 08 Apr Link to this post

    Hello Uwe,

    I am not sure what you mean about OptionSet Box. This is why I made some research and it seems that it is used to show a list of predefined options displayed in a drop-down that a user can choose from by checking/unchecking. In this case, I can suggest several controls from Telerik UI for Winforms suite that might be suitable for you:

    • RadCheckedListBox control - RadCheckedListBox is an enhanced alternative to the standard Windows Forms checked list box control. It displayed a list box in which a checkbox is displayed to the left of each item. 
    • RadListView control - RadListView has built-in checkboxes functionality which can be shown by setting the ShowCheckBoxes property of RadListView to true. You can find more information here.
    • RadCheckedDropDownList control - RadCheckedDropDownList combines RadDropDownList and RadAutoCompleteBox in order to provide the functionality to check items in the drop-down area and tokenize them in the text area.

    Could you please take a look at the suggested controls and consider if any of them should be suitable for you?

    Should you have any other questions do not hesitate to ask.

    Regards,

    Nadya

    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top