If one checkbox is checked, set the other to unchecked

前端 未结 9 1652
Happy的楠姐
Happy的楠姐 2021-01-13 10:06

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

相关标签:
9条回答
  • 2021-01-13 10:37

    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;
    
                }
            }
    
        }
    
    0 讨论(0)
  • 2021-01-13 10:41

    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;
                }
            }
    
    0 讨论(0)
  • 2021-01-13 10:46

    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;
    }
    
    0 讨论(0)
  • 2021-01-13 10:46

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-13 10:46

    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;
        }
    
    0 讨论(0)
  • 2021-01-13 10:55

    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;
            }
    
    0 讨论(0)
提交回复
热议问题