In what order do .NET Windows forms events fire?
I notice that there are a number of different events I can capture when I'm working with a Windows Form in .NET (or any other control, for that matter) - on opening, there's:
- Load
- Activated
- Shown
- VisibleChanged
And when closing, there's:
- Leave
- FormClosed
- FormClosing
- Disposed
Plus any others I've missed. I know I could put a messagebox 开发者_开发知识库in each event, and then run my application and write down the order, but I doubt I'll remember it.
Is there a reference online that lists the -order- in which these events occur, for Forms and other controls? I can't find it on MSDN, though maybe I've missed it somewhere.
This is one of the relevant pages on MSDN :
http://msdn.microsoft.com/en-us/library/86faxx0d.aspx
This is also called the lifecycle of win-forms applications. Every .net technology has a document on these.
Winforms - http://blogs.msdn.com/jaredpar/archive/2007/01/08/windows-forms-event-lifecycle.aspx
Form Startup
- OnHandleCreated
- OnCreateControl
- OnLoad
- OnActivated
- OnShown
Form Shutdown
- OnClosing
- OnClosed
- OnDeactivate
- OnHandleDestroyed
Showing a form:
- Control.HandleCreated
- Control.BindingContextChanged
- Form.Load
- Control.VisibleChanged
- Control.GotFocus
- Form.Activated
- Form.Shown
Closing a form:
- Form.Closing
- Form.FormClosing
- Form.Closed
- Form.FormClosed
- Form.Deactivate
- Control.LostFocus
- Control.HandleDestroyed
- Component.Disposed
I have seen this behavior, may have something to do with MDI:
form.Focus(); // calls OnHandleCreated (this.Handle is x)
form.Show( ); // calls OnLoad (this.Handle is still x), then OnHandleCreated (this.Handle is y)
精彩评论