lambda expression and event handler? - c#

Someone mentioned to me that c# supports to use lambda expression as event handler, can anyone share with me some reference on this?
A code snippet is preferred.

You can use a lambda expression to build an anonymous method, which can be attached to an event.
For example, if you make a Windows Form with a Button and a Label, you could add, in the constructor (after InitializeComponent()):
this.button1.Click += (o,e) =>
{
this.label1.Text = "You clicked the button!";
};
This will cause the label to change as the button is clicked.

try this example
public Form1()
{
InitializeComponent();
this.button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
}
The above event handler can be rewritten using this lambda expression
public Form1()
{
InitializeComponent();
this.button1.Click += (object sender, EventArgs e) = >
{
MessageBox.Show(“Button clicked!”);
};
}

Related

Can I have a have a void inside an event handler declaration?

I have a button on my C# Winform, and the following code:
button1.Click += button1_Click;
and also:
private static void button1_Click(object sender, EventArgs e)
{
// do something
}
I am trying to simplify and reduce the amount of code in my application. Is there any way to do this?
Here's what I am trying to achieve:
button1.Click += void button1_Click(object sender, EventArgs e)
{
// do something
};
This does not work. Is there any other way to achieve this?
You can do this with an anonymous method:
button1.Click += (sender, e) =>
{
// do something
};
But note that you will never be able to unregister this event handler as it is an anonymous method.

Dependency property or just event [duplicate]

Someone mentioned to me that c# supports to use lambda expression as event handler, can anyone share with me some reference on this?
A code snippet is preferred.
You can use a lambda expression to build an anonymous method, which can be attached to an event.
For example, if you make a Windows Form with a Button and a Label, you could add, in the constructor (after InitializeComponent()):
this.button1.Click += (o,e) =>
{
this.label1.Text = "You clicked the button!";
};
This will cause the label to change as the button is clicked.
try this example
public Form1()
{
InitializeComponent();
this.button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
}
The above event handler can be rewritten using this lambda expression
public Form1()
{
InitializeComponent();
this.button1.Click += (object sender, EventArgs e) = >
{
MessageBox.Show(“Button clicked!”);
};
}

Adding event to button programmatically

I want to add an event to a programmatically generated button like this:
Button activityButton = new Button();
activityButton.Click += new EventHandler(onChangeActivityFilter);
I'm getting the following exception in the 2nd line:
Cannot implicit convert type System.EventHandler to System.Windows.RoutedEventhandler
The onChangeActivityFilter methode looks like this:
private void onChangeActivityFilter(object sender, EventArgs e)
{
}
I'd like to know what I'm doing wrong.
You need to create a instance of RoutedEventHandler:
activityButton.Click += new RoutedEventhandler(onChangeActivityFilter);
And also change the method signature:
private void onChangeActivityFilter(object sender, RoutedEventArgs e)
{
}
RoutedEvents where introduced with WPF.
You can also use lambda functions
activityButton.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};

How does addition assignment operator behave

How does addition assignment operator behaves here -
btn.Click += delegate(object sender, EventArgs e)
It adds an event handler to the event Click.
When Click event is raised all the handlers method added to it are called.
For example:
void BtnClickHandler1(object sender, EventArgs e)
{
MessageBox.Show("BtnClickHandler1");
}
void BtnClickHandler2(object sender, EventArgs e)
{
MessageBox.Show("BtnClickHandler2");
}
And you add these methods to Click event like this:
btn.Click += BtnClickHandler1
btn.Click += BtnClickHandler2
When button is clicked the methods will be called in the order you added them, so the message box will be:
BtnClickHandler1
BtnClickHandler2
If you want specific info about += operator, MSDN says:
The += operator is also used to specify a method that will be called
in response to an event; such methods are called event handlers. The
use of the += operator in this context is referred to as subscribing
to an event.
For more info look at:
https://msdn.microsoft.com/en-us/library/edzehd2t%28v=vs.110%29.aspx
http://www.dotnetperls.com/event

Send an EventHandler to a RoutedEventHandleer

here is the basic model of the code I have:
private void textBlock1_Tap (object sender, System.Windows.Input.GestureEventArgs e)
{
TextBox TextBox1 = new TextBox();
TextBlock tblk = (TextBlock)sender;
ApplicationBar = new ApplicationBar();
TextBox1.LostFocus += TextBox1_LostFocus;
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/check.png", UriKind.Relative));
appBarButton.Text = "Accept";
ApplicationBar.Buttons.Add(appBarButton);
appBarButton.Click +=
}
void TextBox1_LostFocus(object sender, RoutedEventArgs e)
{
//do things here
}
I need to subscribe to the click Event, and when it is triggered, I need TextBox1_LostFocus to be called and TextBox1 to be sent as a parameter. Basically what I want to do is make appBarButton.Click do the exact same thing as TextBox1.LostFocus.
Problem is, LostFocus is a RoutedEventHandler and TextBox1_LostFocus takes a RoutedEventArgs as a parameter while appBarButton.Click is an EventHandler.
I'm not very experienced in coding at all so any help is much appreciated!
RoutedEventArgs inherits EventArgs.
You can add the same handler to both events.
Better yet, you can move the code to a function and call it from two different event handlers.

Categories