Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a form displayed?

Tags:

c#

forms

winforms

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?

like image 645
Michael Haddad Avatar asked Mar 10 '23 18:03

Michael Haddad


2 Answers

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.)

like image 94
Patrick Hofman Avatar answered Mar 16 '23 05:03

Patrick Hofman


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 ..

like image 40
BugFinder Avatar answered Mar 16 '23 06:03

BugFinder