Order of events 'Form.Load', 'Form.Shown' and 'Form.Activated' in Windows Forms

99,251

Solution 1

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.

Solution 2

  • 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 call form.Show() (or form.Visible = true).
    If you hide your form, then show it again, Shown will fire again. (But Load 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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
99,251
Ananth
Author by

Ananth

a programmer by choice

Updated on July 08, 2020

Comments