none
Only one checkBox selected RRS feed

  • Question

  • I have all the checkBoxes in the first column selected:

    checkBox1_______checkBox2

    checkBox3_______checkBox4

    checkBox5_______checkBox6

    I would like to have for example if you select checkBox2, checkBox1 will become unchecked and the other way around. And the same for the next row etc.


    • Edited by Thomas224 Tuesday, April 9, 2013 9:12 AM
    Tuesday, April 9, 2013 9:11 AM

Answers

  • If I use this it workes 
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox1.Checked)
                {
                    checkBox2.Checked = false;
                }
            }
    
            private void checkBox2_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox2.Checked)
                {
                    checkBox1.Checked = false;
                }
            }
    I will use them for if checkBox1 checked use data 1 from database if not use data 2 from dababase.
    • Marked as answer by Thomas224 Tuesday, April 9, 2013 9:46 AM
    Tuesday, April 9, 2013 9:39 AM

All replies

  • Use the Checked_Changed event for the checkbox and then set the Checked property to false of the checkbox you want to uncheck

    Tuesday, April 9, 2013 9:19 AM
  • It is better to use RadioButtons for this (users will expect the behaviour). For Windows forms place them in a GroupBox. For WPF set the GroupName property for both RadioButtons to the same name.

    Tuesday, April 9, 2013 9:21 AM
  • If I use this it workes 
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox1.Checked)
                {
                    checkBox2.Checked = false;
                }
            }
    
            private void checkBox2_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox2.Checked)
                {
                    checkBox1.Checked = false;
                }
            }
    I will use them for if checkBox1 checked use data 1 from database if not use data 2 from dababase.
    • Marked as answer by Thomas224 Tuesday, April 9, 2013 9:46 AM
    Tuesday, April 9, 2013 9:39 AM
  • Yep that could work. The reason I mentioned the radio buttons is the from a user experience point of view they will expect to only be able to select one radio button option but will expect to be able to check however many checkboxes they want to.

    Tuesday, April 9, 2013 9:45 AM