In real life, you never call the code you have shown in the Question, because you never create an instance of
System.Windows.Forms.Form
. Instead, you always create a derived class using
Form
as a base class. You create a constructor for your form class. This is where you can setup all event handles, and in most cases should. It will happen exactly as you wanted: before the instance of the form is created.
public class MyForm : Form {
public MyForm {
this.Load += (sender, eventArgs) =&ft; {
MessageBox.Show(
"If you can read this, the form is not yet created. Hope you will see it later",
string.Format(" {0}: before showing the form", Application.ProductName));
};
}
}
You can handle any other form event this way and setup events for all of the form's controls in this way. The event
Form.Load
is kind of "fictional" event, simply invoked before all other form event directly. Even though this event is often handled, it is never absolutely required. Instead, you can simplu call its handler from the Form constructor. More typically, you should setup the event
Form.Shown
,
Form.FormClosing
,
FormFormClosed
. In these cases handling events is essentially important.
—SA