There is a private void btnContBalloon_Click(object sender, EventArgs e). Can I make this static because I want to invoke this from static method but I can not.
3 Answers
Making events static is a great way to shoot the foot. A static event has an unlimited life-time. Which makes any event handlers you register for the event live forever too. Which makes any form that contains such an event handler live forever too. A leak.
Registering an event handler for a static event requires code in, say, the FormClosing event handler that explicitly unregisters the handler. You can see this explicitly documented in the MSDN Library article for the SystemEvents class, one of the few examples of a class in the .NET framework that has static events.
The better approach is to keep track of the form instance whose button's Click event should be activated. Something like this:
public partial class Form1 : Form {
public static Form1 MainForm { get; private set; }
public Form1() {
InitializeComponent();
MainForm = this;
}
public void RunClickMethod() {
button1.PerformClick();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
MainForm = null;
base.OnFormClosing(e);
}
}
Which allows client code to do this:
Form1.MainForm.RunClickMethod();
Yes, if that method doesn't need the instance members of the enclosing class you can make it static. Nothing prevents an event handler from being static, if that is the real question.
Bottom line: If that method only uses the sender object (probably the button) and the event args or other static members, then this is perfectly valid and possible.