If one checkbox is checked, set the other to unchecked

44,299

Solution 1

modify your code as below.

private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
    chkAboveGround.Checked = !chkBuried.Checked;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
    chkBuried.Checked = !chkAboveGround.Checked;
}

Solution 2

I suggest you use check_click instead of check_changed

        private void checkBox1_Click(object sender, EventArgs e)
        {
            checkBox2.Checked = false;
            checkBox3.Checked = false;
        }

        private void checkBox2_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox3.Checked = false;
        }

        private void checkBox3_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox2.Checked = false;
        }

Solution 3

The reason for the behavior you have explained is that you are using CheckedChanged event, which means that when you are setting the Checked property of a CheckBox manually, the event is also fired, causing another box to react again.

Therefore, the following might help you:

private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
    if (chkBuried.Checked == true) {
        chkAboveGround.Checked = false;
    } else {
        chkAboveGround.Checked = true;
    }
}

private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
    if (chkAboveGround.Checked == true) {
        chkBuried.Checked = false;
    } else {
        chkBuried.Checked = true;
    }
}

UPDATE 29.03.2020: functionally the code in my answer is the same as the answer given by Riz. Nevertheless, I am leaving the code as I put it originally since it might make the whole situation easier to understand for the people who are new to coding. If you are to implement anything similar in production code, please use the answer by Riz as an example.

Share:
44,299
Ben
Author by

Ben

"Learn by doing, and Teach through learning. And if you have a question - Ask!" Coder in C#, with basic understanding of Agile principles and other Programming practices. Avid gamer, I have been playing for over 15 years. Amateur Tabletop RP Gamer.

Updated on March 30, 2020

Comments

  • Ben
    Ben about 4 years

    I have two checkboxes on my form; chkBuried and chkAboveGround. I want to set it up so if one is checked, the other is unchecked. How can I do this?

    I have tried the CheckChanged property:

    private void chkBuried_CheckedChanged(object sender, EventArgs e)
    {
        chkAboveGround.Checked = false;
    }
    private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
    {
        chkBuried.Checked = false;
    }
    

    And it works, just not as well as I hoped. That is, when I check chkBuried, then check chkAboveGround, both boxes become unchecked before I can check another one again.

  • Ben
    Ben about 10 years
    Perfect! Thanks Riz :)
  • Ben
    Ben about 10 years
    I tried this, didn't work unfortunately, however your explanation helped me understand why mine wasn't working :) Thanks Danek
  • Gauthier
    Gauthier about 10 years
    It depends on the usage. If the checkboxes are not contained within the same parent you need some way of keeping track of them. If the group has large listing of checkboxes, you will only have to add to the group in one area. If you only have two checkboxes, then this is more then overkill. You did specify two checkboxes, I apologize.
  • dnl-blkv
    dnl-blkv about 10 years
    @Ben, sorry I did not get the question completely correctly. Riz's explanation is really good :)
  • Ben
    Ben about 10 years
    I can see the potential for it, I'm in no way saying this is incorrect, but for this case specifically, I only have two checkboxes on the same form. So for my case it's bit much :)
  • Gauthier
    Gauthier about 10 years
    I would go with DeveloperGuo with the Radioboxes, then can be styled however you want them ( just like checkboxes). Also you not have extra code to handle the checking / unchecking.
  • Ben
    Ben about 10 years
    When I was looking for a solution to my problem, I came acorss this: stackoverflow.com/questions/1410923/… possibly a better use for this code?
  • Ben
    Ben about 10 years
    A new situation has evolved: What if I want to uncheck both boxes? Currently if I check one, then uncheck it, the other is checked again.
  • Riz
    Riz about 10 years
    For the new scenario, you can modify the first handler's body like ... chkAboveGround.Checked = (chkBuried.Checked)? false: chkAboveGround.Checked; and similarly changes for the second handler