What is the difference between Form Load , Form Shown and Form Activated Events ? What is the order in which they are fired ?
The order would be Form Load, which initializes the form and calls the controls, Form Show, 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.
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.
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.