What is the difference between form Form.Load, Form.Shown and Form.Activated events? What is the order in which they are fired?
See the Windows Forms Events Lifecycle:
- Move: This event occurs when the form is moved. Although by default, when a form is instantiated and launched, the user does not move it, yet this event is triggered before the Load event occurs.
- Load: This event occurs before a form is displayed for the first time.
- VisibleChanged: This event occurs when the Visible property value changes.
- Activated: This event occurs when the form is activated in code or by the user.
- Shown: This event occurs whenever the form is first displayed.
- Paint: This event occurs when the control is redrawn.
- Deactivate: This event occurs when the form loses focus and is not the active form.
- Closing: This event occurs when the form is closing.
- Closed: This event occurs when the form is being closed.
The
Load
event fires when the form has been initialized, after its handle has been created but before it is shown.The
Shown
event fires after the first time the form becomes visible, when you callform.Show()
(orform.Visible = true
).
If you hide your form, then show it again,Shown
will fire again. (ButLoad
won't)The
Activate
event fires when the user switches to your form.
If the user switches to a different program (or form), then switches back to your form,Activate
will fire again.
ShowDialog
twice, Shown
fires twice. –
Gravelly Shown
should really only fire once. After you call ShowDialog
, isn't the form closed (or anyway, shouln't it be)? I feel like that might be an atypical scenario, calling ShowDialog
twice. –
Bullshit Load
will fire every time you call ShowDialog
on a form after closing it. This is easy to reproduce: var f = new Form(); f.Load += (s, e) => MessageBox.Show("Loaded"); f.ShowDialog(); f.ShowDialog();
. And it's not such an atypical scenario to call ShowDialog
multiple times. Complex forms can take several seconds to initialize, so it makes sense to close and reuse them, rather than dispose and recreate new instances each time. –
Pernick Moreover, Form.Activate
event can be fired multiple times. For example, if you open a message box from your form, and when you click on the messagebox's any button, and return back to the form, Form.Activate
is fired. The same is true for any other dialog box such as FileOpenDialog
.
The Form and Control classes expose a set of events related to application startup and shutdown. When a Windows Forms application starts, the startup events of the main form are raised in the following order:
Control.HandleCreated
Control.BindingContextChanged
Form.Load
Control.VisibleChanged
Form.Activated
Form.Shown
When an application closes, the shutdown events of the main form are raised in the following order:
Form.Closing
Form.FormClosing
Form.Closed
Form.FormClosed
Form.Deactivate
Focus and Validation Events
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ActiveControl property to the current form, focus events of the Control class occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
When you change the focus by using the mouse or by calling the Focus method, focus events of the Control class occur in the following order:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
The order would be Form.Load
, which initializes the form and calls the controls, Form.Shown
, which marks the frame as visible (even in C++, this is done after the form is created), and Form.Activated
, which gives the forum focus.
© 2022 - 2024 — McMap. All rights reserved.