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

5

53

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

Rosalvarosalyn answered 18/6, 2010 at 13:53 Comment(3)
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!Maim
@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.Macro
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.Rosalvarosalyn
K
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.
Karlakarlan answered 18/6, 2010 at 13:56 Comment(2)
Here's the MSDN link for the Order of Events in Windows Forms.Adjutant
Does the Activated events creates a new instance of the form ?Marilee
G
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.

Gravelly answered 18/6, 2010 at 13:57 Comment(5)
This is not accurate, Shown only fires once.Macro
@Hans: Wrong. I just tried it. If you call ShowDialog twice, Shown fires twice.Gravelly
Hmm, not sure what you are doing. Load fires twice. Not disposing a dialog is usually a bug.Macro
According to the MSDN documentation, 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
K
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.

Kozlowski answered 30/6, 2010 at 3:7 Comment(2)
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.Tricksy
Does the Activated event creates a new instance of the form ?Marilee
Q
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
Quoits answered 7/4, 2016 at 13:0 Comment(0)
C
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.

Codee answered 18/6, 2010 at 13:54 Comment(2)
This is not right..I used Debug.WriteLine - The output windows shows the sequence of events as Load, Activated, and finally Shown.Rosalvarosalyn
What do you mean by "calling a control"?Villalba

© 2022 - 2024 — McMap. All rights reserved.