0

This works in adding an event handler in C# WPF

CheckBox ifPrint = new CheckBox();
ifPrint.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(
(sender, e) => //toggle check box event
{
    //do stuff
}));

but it looks messy when the method body gets long, so I want to define the method elsewhere like this

ifPrint.AddHandler(CheckBox.ClickEvent, delegate(object sender, RoutedEventArgs e){
    checkBoxClick(sender, e);
});

private void checkBoxClick(object sender, RoutedEventArgs e)
{
//do stuff
}

but this doesn't even compile with the error: Cannot convert anonymous type to type 'System.Delegate' because it is not a delegate type

Sorry, I am new to this and have no idea how it's supposed to be done. Is this even close? Thanks!

totoro
  • 2,837
  • 5
  • 28
  • 53

2 Answers2

3

You can subscribe to a separate method like this, as long as the signature of checkBoxClick is correct:

ifPrint.Click += checkBoxClick;

You can also subscribe to an event inline like this:

ifPrint.Click += (s, e) => SomeMethod();

Which then allows you to name your method something more reasonable and not require it to accept parameters:

private void SomeMethod()
{
    //do stuff
}

Just to explain it a little further, in the above code, s and e take the place of the parameters in your checkBoxClick event method, so it's basically equivalent to this:

ifPrint.Click += checkBoxClick;

private void checkBoxClick(object sender, RoutedEventArgs e)
{
    SomeMethod();
}

Edit, in regards to your comment.

Given this is much simpler, when, if ever, should one use this? ifPrint.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler( (sender, e) => { //do stuff }));

I honestly don't think I've ever used that syntax.

It seems that in most cases it does the same thing. According to the MSDN docs, there's a handledEventsToo parameter on the AddHandler() method, which I think could be significant.

Imagine you subscribed to an event multiple times, like this:

ifPrint.Click += checkBoxClick;
ifPrint.Click += checkBoxClick;
ifPrint.Click += checkBoxClick;

And inside your event, you set e.Handled = true. If you didn't have that line, you'd see the message box displayed 3 times. But with that line, you only get the message box once, because the first time the event fires, it marks the event "handled".

private void checkBoxClick(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Clicked!");

    e.Handled = true;
}

By passing in true for the last parameter (it's false by default), you actually tell it to fire that event, even if other events already "handled" the event.

ifPrint.AddHandler(CheckBox.ClickEvent,
    new RoutedEventHandler((s, e) => { /* do stuff */ }), true);
Community
  • 1
  • 1
Grant Winney
  • 61,140
  • 9
  • 100
  • 152
  • thanks! Given this is much simpler, when, if ever, should one use this? `ifPrint.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler( (sender, e) => { //do stuff }));` – totoro Oct 14 '14 at 03:13
1

try this logic to attach click event handler for your checkbox.

CheckBox ifPrint = new CheckBox();
ifPrint.Click+=checkBoxClick;


private void checkBoxClick(object sender, RoutedEventArgs e)
{
//do stuff
}
jadavparesh06
  • 886
  • 7
  • 11