This is a migrated thread and some comments may be shown as answers.

multiple checkboxes allow one check at a time

4 Answers 261 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 27 Apr 2011, 02:33 PM

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.
 

4 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 02:22 PM
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
0
aditya
Top achievements
Rank 1
answered on 21 Oct 2018, 03:23 AM

 

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

0
Uwe
Top achievements
Rank 1
answered on 04 Apr 2020, 02:00 PM
Is there anything like a OptionSet Box?
0
Nadya
Telerik team
answered on 08 Apr 2020, 10:50 AM

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
Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Scott
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
aditya
Top achievements
Rank 1
Uwe
Top achievements
Rank 1
Nadya
Telerik team
Share this question
or