Reputation:
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 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.
Upvotes: 32
Views: 31134
Reputation: 1149
private void cbSuspended_CheckedChanged(object sender, EventArgs e)
{
var result = MessageBox.Show("Are you sure?", "Change status", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result != DialogResult.Yes)
{
cbSuspended.CheckedChanged -= cbSuspended_CheckedChanged;
cbSuspended.Checked = !cbRemoved.Checked;
cbSuspended.CheckedChanged += cbSuspended_CheckedChanged;
return;
}
}
Upvotes: 0
Reputation: 289
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);
}
Upvotes: 1
Reputation: 23123
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; } } }
Upvotes: 21
Reputation: 19790
Set AutoCheck to false.
Override OnClick to manual check the checkbox
Upvotes: 48