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
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.
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");
};
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.
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!”);
};
}
I am working on a winforms app and I have added some controls dynamically (eg. Button). I want to add an event to that created button; how can I perform this? Also, can someone refer a C# book to me which covers all winforms topics?
// create some dynamic button
Button b = new Button();
// assign some event to it
b.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
// add the button to the form
Controls.Add(b);
I totally agree with Darin's answer, and this is another syntax of adding dynamic event
private void Form1_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Click += new EventHandler(ShowMessage);
Controls.Add(b);
}
private void ShowMessage(object sender,EventArgs e)
{
MessageBox.Show("Message");
}