获取复选框未选中状态

本文关键字:状态 复选框 获取 | 更新日期: 2023-09-27 18:16:12

我需要实现一个复选框,以在两种方法之间切换启用/禁用某些控件。我正在使用以下代码,我也尝试了其他方法,但没有运气。

  private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
                if (checkBox1.Checked)//this is working
                {
                    trackBar2.Enabled = false;
                    button3.PerformClick();
                    textBox8.Enabled = true;
                }
            else// this is supposed to work if checkbox is unchecked but doesn't work
            {
                trackBar2.Enabled = true;
                textBox8.Enabled = false;
            }
        }

得到的结果总是相同的。如果我选中复选框,第一个条件满足,就没问题。如果我取消选中文本框,什么也不会发生,也不会回到第一个条件。如何检测已检查/未检查的条件?

获取复选框未选中状态

你也可以这样写:

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        trackBar2.Enabled = !checkBox1.Checked;
        textBox8.Enabled = checkBox1.Checked;
        if (checkBox1.Checked)
        {
            button3.PerformClick();
        }
    }

我认为你应该把if(checkBox1.Checked == false)添加到else:

 private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)//this is working
        {
                trackBar2.Enabled = false;
                button3.PerformClick();
                textBox8.Enabled = true;
        }
        else if(checkBox1.Checked == false)
        {
            trackBar2.Enabled = true;
            textBox8.Enabled = false;
        }
    }