Ben
Ben

Reputation: 2523

If one checkbox is checked, set the other to unchecked

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.

Upvotes: 5

Views: 48924

Answers (10)

adnan Alyaari
adnan Alyaari

Reputation: 61

I'v Primitive method usually use it by defination 2 integer guide to prevent infinity loop when change checke box state:

int guide1=0; //one time to checkbox1 event
int guide2=0; //one time to checkbox2 event
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
 if(guide1==0)
 { 
  chkAboveGround.Checked = false;
  chkBuried.Checked = true;
    guide1=1;
    guide2=0;
  }

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

Upvotes: 0

dnl-blkv
dnl-blkv

Reputation: 2128

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.

Upvotes: 2

Patricio Díaz
Patricio Díaz

Reputation: 1

I needed to show or not show a table when activating the CheckBox, how they were two, if I activated both, everything was fine, but if I tried to deactivate one later, the other was also deactivated. PD: The default value for the tables that I used was Visible=false. The solution I got was the following:

 protected void YourNameOfMethodForBothCheckBox(object sender, EventArgs e)
    {
        if (CheckBox_1.Checked == true)
        {
            Table_1.Visible = true;

            if (CheckBox_2.Checked == true)
            {
                Table_2.Visible = true;
            }
            else { Table_2.Visible = false; }


        }
        else
        {
            Table_1.Visible = false;

            if (CheckBox_2.Checked == false)
            {
                Table_2.Visible = false;
            }
            else
            {
                Table_2.Visible = true;

            }
        }

    }

Upvotes: 0

RittenRA
RittenRA

Reputation: 33

This will work for two unchecked boxes, since they are already unchecked it is simpler.
I had to do this myself also.

private void customer_IsCheckedChanged(object sender, EventArgs e)
{
    business.IsChecked = false;
}

private void business_IsCheckedChanged(object sender, EventArgs e)
{
    customer.IsChecked = false;
}

Upvotes: 0

Michel
Michel

Reputation: 1

The best option and easiest way for me was to create a Click event instead of a CheckedChanged event. This method works perfectly with two or more checkbox and allows to have them all unchecked.

    private void chkOne_Click(object sender, EventArgs e)
    {
        chkTwo.Checked = false;
    }

    private void chkTwo_Click(object sender, EventArgs e)
    {
        chkOne.Checked = false;
    }

Upvotes: 0

ILoveAMac
ILoveAMac

Reputation: 14

Better version, allows the user to uncheck boxes.

        private void chkOne_CheckedChanged(object sender, EventArgs e)
        {
            if (chkTwo.Checked == true)
            {
                chkTwo.Checked = !chkOne.Checked;
            }
        }

        private void chkTwo_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOne.Checked == true)
            {
                chkOne.Checked = !chkTwo.Checked;
            }
        }

Upvotes: 0

Syamilsynz
Syamilsynz

Reputation: 51

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;
        }

Upvotes: 5

Gauthier
Gauthier

Reputation: 1321

List<CheckBox> groupOfCheckBoxes = new List<CheckBox>();
void InitFunction() {
    groupOfCheckBoxes.Add(checkbox1);
    groupOfCheckBoxes.Add(checkbox2);
    groupOfCheckBoxes.Add(checkbox3);
    foreach (CheckBox cb in groupOfCheckBoxes)
     cb.Click += checkbox_Click
}
void checkbox_Click(object sender, EventArgs e) 
{
 foreach (CheckBox cb in groupOfCheckBoxes) {
  cb.IsChecked = cb == sender;
 }
}

However I would suggest radio boxes as well. The code above is untested and may have some typos

Upvotes: 0

tweellt
tweellt

Reputation: 2161

I would prefer radio buttons, but you can do something like this:

public void CheckACheckBox(Checkbox ck)
{
    foreach (Control ckb in this.Controls)
    {
       if ((ckb is CheckBox) && (ckb == ck))
          ck.Checked = true;
       else
          ck.Checked = false;
    }
}

Upvotes: 0

Riz
Riz

Reputation: 1131

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;
}

Upvotes: 12

Related Questions