locked
How can i disbale unchecking of 2 checkboxes ? RRS feed

  • Question

  • User1253338400 posted

    Hi 

    I have 2 checkboxes on a winform . How can i not allow the user to deselect a chekc box if both arent selected .. 

    In other words one checkbox has to be selected at all times

    thanks

    Thursday, April 30, 2020 6:50 AM

Answers

  • User288213138 posted

    Hi ribby32,

    I have 2 checkboxes on a winform . How can i not allow the user to deselect a chekc box if both arent selected .. 

    In other words one checkbox has to be selected at all times

    According to your description, i made demo for you as a reference.

    public Form1()
            {
                InitializeComponent();
                checkBox1.Checked = true;
                checkBox1.CheckedChanged += CheckBox_CheckedChanged;
                checkBox2.CheckedChanged += CheckBox_CheckedChanged;
            }
    
            private void CheckBox_CheckedChanged(object sender, EventArgs e)
            {
                if((checkBox1.Checked || checkBox2.Checked) == false)
                {
                    ((CheckBox)sender).Checked = true;
                }
            }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2020 9:33 AM

All replies

  • User288213138 posted

    Hi ribby32,

    I have 2 checkboxes on a winform . How can i not allow the user to deselect a chekc box if both arent selected .. 

    In other words one checkbox has to be selected at all times

    According to your description, i made demo for you as a reference.

    public Form1()
            {
                InitializeComponent();
                checkBox1.Checked = true;
                checkBox1.CheckedChanged += CheckBox_CheckedChanged;
                checkBox2.CheckedChanged += CheckBox_CheckedChanged;
            }
    
            private void CheckBox_CheckedChanged(object sender, EventArgs e)
            {
                if((checkBox1.Checked || checkBox2.Checked) == false)
                {
                    ((CheckBox)sender).Checked = true;
                }
            }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2020 9:33 AM
  • User-474980206 posted

    this seems like poor UI design. either a validation message, or use a radio list with the third option, all of the above.

    Thursday, April 30, 2020 3:32 PM