Reputation: 4435
I am trying to figure out when exactly the Form.Load
event occurs. In MSDN it sais:
Occurs before a form is displayed for the first time.
But when the form is displayed for the first time? My first instinct was immediately after InitializeComponent()
, but when I tried the following code, the MessageBox
showed 5
even though the value was set after InitializeComponent()
, so it's not immediately after InitializeComponent()
:
public partial class Form1 : Form
{
private int number;
public Form1()
{
InitializeComponent();
number = 5;
}
public void Form_Load(object sender, EventArgs e)
{
MessageBox.Show(number);
}
}
So When does it occurs?
Upvotes: 4
Views: 87
Reputation: 17858
Have a read of https://msdn.microsoft.com/en-us/library/86faxx0d(v=vs.110).aspx
This explains the order of startup and shutdown of forms.
In short, a number of events are triggered in order - eg create...load...activate..shown ..
Upvotes: 2
Reputation: 156898
OnLoad
is one of the methods being called when you call Show
or ShowDialog
on a Form
.
The first time you call Show
or ShowDialog
, OnLoad
is called and your Load
event is fired. (Just like OnHandleCreated
, etc.)
Upvotes: 5