.net 如何取消 RadioButton 或 CheckBox 选中的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1371715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:16:24  来源:igfitidea点击:

How to cancel RadioButton or CheckBox checked change

.netwinformsevents

提问by RvdK

Is there any way to cancel a RadioButton or CheckBox's change in state before it changes?

有没有办法在 RadioButton 或 CheckBox 更改之前取消它的状态更改?

I was hoping for a simple event like CheckedChanging or BeforeCheckedChange.

我希望有一个像 CheckedChanging 或 BeforeCheckedChange 这样的简单事件。

I really want to avoid listening for mouse clicks and key presses. But if you know of a proven reliable way of using mouse clicks and key presses, please explain it.

我真的想避免监听鼠标点击和按键。但是,如果您知道使用鼠标点击和按键的可靠方法,请解释一下。

I'm using .NET 2.0 with standard Winforms controls.

我将 .NET 2.0 与标准 Winforms 控件一起使用。

回答by RvdK

Set AutoCheck to false.

将自动检查设置为 false。

Override OnClick to manual check the checkbox

覆盖 OnClick 以手动检查复选框

回答by Corey Trager

Code demo's AutoCheck, adds a confirmation prompt to Click event.

代码演示的 AutoCheck,为 Click 事件添加了确认提示。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.checkBox1.AutoCheck = false;
        this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
    }

    private void checkBox1_Click(object sender, EventArgs e)
    {
        CheckBox checkBox = (CheckBox)sender;
        if (!checkBox.Checked)
        {
            DialogResult dialogResult = MessageBox.Show(
                "Are you sure?", "my caption",
                 MessageBoxButtons.YesNo);

            if (dialogResult == DialogResult.Yes)
            {
                checkBox.Checked = true;
            }
        }
        else
        {
            checkBox.Checked = false;
        }
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.checkBox1.AutoCheck = false;
        this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
    }

    private void checkBox1_Click(object sender, EventArgs e)
    {
        CheckBox checkBox = (CheckBox)sender;
        if (!checkBox.Checked)
        {
            DialogResult dialogResult = MessageBox.Show(
                "Are you sure?", "my caption",
                 MessageBoxButtons.YesNo);

            if (dialogResult == DialogResult.Yes)
            {
                checkBox.Checked = true;
            }
        }
        else
        {
            checkBox.Checked = false;
        }
    }
}

回答by Jon649

I used this to cancel a radio button check.

我用它来取消单选按钮检查。

 private void radioButton1_MouseClick(object sender, MouseEventArgs e)
    {
        RadioButton r = (RadioButton)sender;
        r.Checked = !(r.Checked);
    }