tags:

views:

335

answers:

4

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

A: 

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.

Stefan Valianu
This is not right..I used Debug.WriteLine - The output windows shows the sequence of events as Load, Activated, and finally Shown.
Ananth
+2  A: 

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
+1  A: 
  • 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
This is not accurate, Shown only fires once.
Hans Passant
@Hans: Wrong. I just tried it. If you call `ShowDialog` twice, `Shown` fires twice.
SLaks
Hmm, not sure what you are doing. Load fires twice. Not disposing a dialog is usually a bug.
Hans Passant
@SLaks: 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
+1  A: 

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.

devcoder