Apr 29
Sometimes it’s important to understand the order of events that occur when a WinForms Form is opened, closed, shown or hidden. There are also a few “gotchas” that are important to know.
Form Open
Following is the order of events when a Form is opened. Note that the Form’s Visible and IsDisposed property values are listed when each event is raised:
Event | Visible | IsDisposed |
call form.Show() method | false | false |
Control.HandleCreated | false | false |
Control.BindingContextChanged | true | false |
Form.Load | true | false |
Control.VisibleChanged | true | false |
Control.GotFocus | true | false |
Form.Activated | true | false |
Form.Shown | true | false |
Things to note:
- The Form becomes Visible starting with the BindingContextChanged event.
- The GotFocus event will typically not fire if the form contains controls, as one of these controls will likely get focus when the form is shown.
Form Close
This is the order of events when a Form is closed:
Event | Visible | IsDisposed |
call form.Close() method | true | false |
Form.Closing | true | false |
Form.FormClosing | true | false |
Form.Closed | true | false |
Form.FormClosed | true | false |
Form.Deactivate | true | false |
Control.LostFocus | true | false |
Control.HandleDestroyed | true | false |
Component.Disposed | false | false |
after Disposed event | false | true |
Things to note:
- The Form is no longer Visible starting with the Disposed event.
- This means that the Form is still Visible during the FormClosed event!
- The Closing and Closed events were marked obsolete in .NET 2.0. You should use the FormClosing and FormClosed events instead.
- The Closing and Closed events are not raised when the Application.Exit method is called to exit your application. Which is just another reason why you should avoid using these events.
- You can prevent the Form from closing during the Closing event.
- The Deactivate and LostFocus events are raised only if the Form had focus when it was closed.
- The Form is not marked as IsDisposed until after the Disposed event.
Form Hide
This is the order of events when a Form is hidden:
Event | Visible |
set form.Visible = false | true |
Form.Deactivate | true |
Control.LostFocus | true |
Control.VisibleChanged | false |
Things to note:
- The Form is no longer Visible starting with the VisibleChanged event.
- The Deactivate and LostFocus events are raised only if the Form had focus when it was hidden.
Form Show
This is the order of events when a hidden Form is shown:
Event | Visible |
set form.Visible = true | false |
Control.VisibleChanged | true |
Control.GotFocus | true |
Form.Activated | true |
Things to note:
- The Form becomes Visible starting with the VisibleChanged event.
- The GotFocus event will typically not fire if the form contains controls, as one of these controls will likely get focus when the form is shown.
- When hiding or closing a form, Deactivate is raised, then LostFocus. But when showing a form, the order is reversed: GotFocus is raised, then Activated. These events are also raised when a form loses and gains focus.
thnks really helpfull
Useful information. Thank you.
Very useful! thanks!
Allows me to understand forms better.
I have a windows Application using C#.Net. In this the Form Load event is not firing. I placed a debuger but the control never goes to Form Load event.What could be the problem. Infact none of the control events are getting fired.
Can you guess wht could be the problem?
Very useful – thanks!
Nice but imprecisely. How about “Resize” event?
This is really help-full! I’v been looking for something like this for quite some time now.
Du you have a simular event sequence for when a form is being designed in VS2010?
Hi,
It’s very helpful, but one remark about Form.Open:
According to the MSDN link for Control.HandleCreated Event
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handlecreated%28v=vs.90%29.aspx
During this event the Visible property should be a true.
Concrete and helpful