52

What is the difference between form Form.Load, Form.Shown and Form.Activated events? What is the order in which they are fired?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ananth
  • 10,330
  • 24
  • 82
  • 109
  • 28
    NEVER, EVER, EVER COUNT ON THE ORDER OF EVENTS IN WINFORMS! In fact, use as few events if possible, and if/when you use many, they should call just a few common subs that do most of the work. True, the order of events won't change, but that style of programming is asking for bugs (speaking from my own and unfortunately mostly others' experience). And don't ever shout in a StackOverflow comment, either! – FastAl Jun 18 '10 at 14:32
  • 20
    @Anthony: MessageBox is a *great* way to mess up the event order. It will make the Shown event run *before* the Load event ends. Never debug UI events with MessageBox, Debug.WriteLine() is best. – Hans Passant Jun 18 '10 at 15:14
  • 4
    I used Debug.WriteLine - thanks Hans :) in Load, Shown and Activated event. The output windows shows the sequence of events as Load, Activated, and finally Shown. – Ananth Jun 21 '10 at 11:41

5 Answers5

74

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.
Galwegian
  • 41,475
  • 16
  • 112
  • 158
24
  • 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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    This is not accurate, Shown only fires once. – Hans Passant Jun 18 '10 at 15:17
  • 2
    @Hans: Wrong. I just tried it. If you call `ShowDialog` twice, `Shown` fires twice. – SLaks Jun 18 '10 at 16:36
  • 2
    Hmm, not sure what you are doing. Load fires twice. Not disposing a dialog is usually a bug. – Hans Passant Jun 18 '10 at 16:57
  • 4
    According to the [MSDN documentation](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown.aspx), `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. – Dan Tao Jun 30 '10 at 03:12
  • 1
    `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. – Douglas Nov 23 '15 at 21:12
15

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.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
matrix
  • 3,000
  • 3
  • 24
  • 35
  • Also: Haven't actually tested this in .net to be sure if it behaves the same, but I know in vb6 that if you are stepping through the code a line at a time, it will hit the form load event, but not the activate event which can be misleading when you don't understand why. – Brandon Moore Feb 26 '12 at 21:17
  • 1
    Does the Activated event creates a new instance of the form ? – Yash Saraiya Jan 07 '16 at 15:38
8

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
Taja_100
  • 453
  • 6
  • 15
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefan Valianu
  • 1,370
  • 2
  • 13
  • 24