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?
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.)
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 ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With