How to cancel RadioButton or CheckBox checked change

后端 未结 3 1780
清歌不尽
清歌不尽 2021-01-03 19:10

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

I was hoping for a simple event like CheckedChanging or BeforeCheckedChang

相关标签:
3条回答
  • 2021-01-03 19:42

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

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-03 19:44

    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);
        }
    
    0 讨论(0)
  • 2021-01-03 19:54

    Set AutoCheck to false.

    Override OnClick to manual check the checkbox

    0 讨论(0)
提交回复
热议问题