简体   繁体   English

单击另一个复选框时禁用一个复选框

[英]Disabling a checkbox when another checkbox is clicked

I am trying to disable the thin and crispy checkbox when traditional checkbox is clicked.当单击传统复选框时,我试图禁用薄而脆的复选框。 I have these in a group due to me enabling the whole group when the numericUpDown value is set to 1. When I click traditional checkbox, it doesn't disable the thin and crispy checkbox.由于我在 numericUpDown 值设置为 1 时启用了整个组,因此我将它们放在一个组中。当我单击传统复选框时,它不会禁用薄而脆的复选框。 I am using windows form application我正在使用 windows 表单应用程序

Code代码

        private void NudQuantity1_ValueChanged(object sender, EventArgs e)
        {

            if (NudQuantity1.Value == 0)
            {
                gbCheesePizza.Enabled = false;
            }
            else
            {
                gbCheesePizza.Enabled = true;
            }

            if (CBXTraditional1.Checked == true)
            {
                CBXthinandcrispy1.Enabled = false;
            }

        }

When I run this code outside of a groupbox, it works perfectly.当我在组框之外运行此代码时,它可以完美运行。

I don't think this block should be inside the event handler我认为这个块不应该在事件处理程序中

if (CBXTraditional1.Checked == true)
{
    CBXthinandcrispy1.Enabled = false;
}

It means that, provided you've got no other event handling for the checkboxes, this code will only be executed when you change the value of NudQuantity1 so it won't execute anything when you click the checkboxes afterwards.这意味着,如果您没有对复选框进行其他事件处理,则仅当您更改NudQuantity1的值时才会执行此代码,因此当您之后单击复选框时它不会执行任何操作。

Try use radio buttons as Steve mentioned.尝试使用史蒂夫提到的单选按钮。 They do this for you.他们为你做这件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM