简体   繁体   中英

When is a form displayed?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM