Reputation: 549
Is there any method or Is it possible to write a method to execute a set of instructions when a (Windows Form) checkbox is unchecked (but not when checked)? I will clarify this providing my sample code.
Existing Sample Code:
private void ChkBox_CheckedChanged(object sender, EventArgs e)
{
if (ChkBox.Checked == false)
{
MessageBox.Show("unchecked");
}
}
Q: Could that if condition
be avoided by writing a method for this which gets executed only when checkbox is unchecked.
Illustration: Something like this:
private void ChkBox_Unchecked(object sender, EventArgs e)
{
MessageBox.Show("unchecked");
}
additional comments: I don't have anything to execute for event 'checkbox is checked' .. So just thinking if I can avoid checkchanged
event by replacing it with unchecked
type of event.
Upvotes: 5
Views: 42075
Reputation: 547
This will be a simple straight forward solution although the best suggested way will be to inherit from the CheckBox and modify the events. But, a relatively simple way is as follows:
Reduce the CheckBox
width so that the CheckBox Label
is no longer visible. Only the box should be visible. Then, include another label beside the box which should be mutually exclusive from the box.
Now, edit the CheckBox Clicked
event. Use some sort of a flag to identify the state of the checkbox. Voila!
Upvotes: 0
Reputation: 20565
You can inherit the CheckBox
and override OnCheckedChanged
class ExtendedCheckBox : CheckBox
{
public event EventHandler Unchecked;
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if (Checked == false)
OnUnchecked(e);
}
}
Upvotes: 2
Reputation: 236188
If you really need that strange events, then create custom check box by inheriting from CheckBox. Override it's OnCheckedChanged
event, and add your Unchecked
event:
public class StrageCheckBox : CheckBox
{
public event EventHandler Unchecked;
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if (!Checked)
OnUnchecked(e);
}
protected virtual void OnUnchecked(EventArgs e)
{
if (Unchecked != null)
Unchecked(this, e);
}
}
Use StrangeCheckBoxes
instead of default check boxes. Subscribe to Unchecked
event. Voila, you have that strange behavior.
Remark Actually I would not suggest usage of custom control or if/else in all scenarios. If you have many checkboxes with this kind of behavior, then custom control is better than many duplicated if/else in each handler. If you have several controls, or single handler for all checkboxes, then I'd go with if/else approach.
Upvotes: 6
Reputation: 3687
I don't think you can do something like this, but if you have many instructions to execute if the checkbox was unchecked and you don't want to encapsulate them in an if
statement you could check for checked
at the beginning and return:
private void ChkBox_CheckedChanged(object sender, EventArgs e)
{
if (ChkBox.Checked) return;
MessageBox.Show("unchecked");
}
Upvotes: 0
Reputation: 1655
No, sorry. That's the only event you can trigger for a state change. Your code looks fine. Any reason you don't like it?
Upvotes: 2
Reputation: 1690
As far as I know the event is fired on all changes, you could write your own control which differentiates the value selected and raises separate events but not sure it is really worthwhile.
Upvotes: 0
Reputation: 45083
No, the only event exposed is one to detect when a change to the state occurred, then you must check the state to determine how it changed.
You could, if so inclined, create your own control which essentially wraps this one and exposes such state-distinguishing events, but this seems entirely pointless.
Upvotes: 1