How do I call an event method in C#?

How do I call button1_click method from button2_click? Is it possible?

Its wholly possible to invoke the button's click event, but its a bad practice. Move the code from your button into a separate method. For example:

protected void btnDelete_OnClick(object sender, EventArgs e)
{
    DeleteItem();
}

private void DeleteItem()
{
    // your code here
}

This strategy makes it easy for you to call your code directly without having to invoke any event handlers. Additionally, if you need to pull your code out of your code behind and into a separate class or DLL, you're already two steps ahead of yourself.


// No "sender" or event args
public void button2_click(object sender, EventArgs e)
{
   button1_click(null, null);
}

or

// Button2's the sender and event args
public void button2_click(object sender, EventArgs e)
{   
   button1_click(sender, e);
}

or as Joel pointed out:

// Button1's the sender and Button2's event args
public void button2_click(object sender, EventArgs e)
{   
   button1_click(this.button1, e);
}

You don't mention whether this is Windows Forms, ASP.NET, or WPF. If this is Windows Forms, another suggestion would be to use the button2.PerformClick() method. I find this to be "cleaner" since you are not directly invoking the event handler.