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.
Related
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!”);
};
}
How can I call this event private void panel1_Paint(object sender, PaintEventArgs e){.....} from like button click function or something?
I've tried panel1.Paint += new PaintEventHandler(panel1_Paint); and couple more but they did not seem to work.
try this:
private void Button1_Click(object sender, EventArgs e){
panel1.Invalidate();
}
the Invalidate() Method force the control to repaint.
You need to use it as simple function:
...your_code...; panel1_Paint(null, null); ...your_code...
Hello I have this code:
private void txtNumero_KeyDown(object sender, KeyEventArgs e)
{
CercaCliente();
}
private void txtNote_KeyDown(object sender, KeyEventArgs e)
{
CercaCliente();
}
private void txtNominativo_KeyDown(object sender, KeyEventArgs e)
{
CercaCliente();
}
How can I write this code in better mode? Thanks
there are two approaches you can choose from, and it depends what type of effort you are ready to do...
first when you bind the event there itself call the method rather than creating handler method for each.
Currently, you are doing -
txtNumero.KeyDown += new txtNumero_KeyDown;
..
..
txtNote.KeyDown += new txtNumero_KeyDown;
then in your method you are calling this common method 'CercaCliente()'. you can directly use func delegate to call you common method. e.g.
txtNumero.KeyDown += (o,e)=>CercaCliente();
..
..
txtNote.KeyDown += (o, e)=>CercaCliente();
OR
You can create custom control, derived from textbox, and there you can handle it.
Add this common method
private void HandlerMethod(object sender, EventArgs e)
{
CercaCliente();
}
Then inside your form load Attach this handler method to all events
this.txtNominativo.KeyDown += HandlerMethod;
this.txtNote.KeyDown += HandlerMethod;
this.txtNumero.KeyDown += HandlerMethod;
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");
};
I have populated a datagrid but would like an event to fire when the user double clicks the row and highlights it. I have looked through all the properties but cannot seem to work out which one it is.
I've also looked on MSDN but also cannot see anything.
Many Thanks,
Sam
Try to use the code below:
this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
}
The problem with CellDoubleClick is it won't fire an event for keyboard users when navigating with arrows.
Instead, some other events you can handle are:
RowEnter
SelectionChanged
RowStateChanged
You can poke around when each fires by attaching handlers to each event and writing to the output window:
dataGridView1.RowEnter += dgv_RowEnter;
dataGridView1.SelectionChanged += dgv_SelectionChanged;
dataGridView1.RowStateChanged += dgv_RowStateChanged;
dataGridView1.CellContentClick += dgv_CellContentClick;
private void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)
{
Debug.WriteLine("RowEnter");
}
private void dgv_SelectionChanged(object sender, EventArgs e)
{
Debug.WriteLine("SelectionChanged");
}
private void dgv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
Debug.WriteLine("RowStateChanged");
}
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Debug.WriteLine("CellContentClick");
}