2009-10-15 77 views
4

我注意到有一些不同的事件時,我有一個Windows窗體在.NET中工作(或任何其他的控制,對於這個問題)我可以捕獲 - 在開放,有:.NET Windows窗體事件觸發的順序是什麼?

  • 負載
  • 激活
  • 所示
  • VisibleChanged

和關閉時,有:

  • 離開
  • FormClosed
  • 的FormClosing
  • 處理完畢

另外,我已經錯過了任何人。我知道我可以在每個事件中放置一個消息框,然後運行我的應用程序並寫下訂單,但我懷疑我會記住它。

有沒有列出這些事件發生-order-,窗體和其他控件的基準線上?我無法在MSDN上找到它,但也許我錯過了它的某個地方。

回答

7

這也是所謂的雙贏,表單應用程序的生命週期。每個.net技術都有一個關於這些的文檔。

的WinForms - http://blogs.msdn.com/jaredpar/archive/2007/01/08/windows-forms-event-lifecycle.aspx

形式啓動

  1. OnHandleCreated
  2. OnCreateControl
  3. 的OnLoad
  4. OnActivated
  5. OnShown

表格關機

  1. OnClosing
  2. OnClosed
  3. OnDeactivate
  4. OnHandleDestroyed
+2

+1提到「生命週期」 - 知道魔術關鍵字是一個巨大的幫助,當搜索 – overslacked 2009-10-16 05:07:18

+0

我發現在特殊情況下可以OnCreateControl之前調用Onload。這種情況是Contorl.CreateGraphics()。 – qub1n 2014-04-15 07:33:07

2

顯示的一種形式:

  1. Control.HandleCr eated
  2. Control.BindingContextChanged
  3. Form。負載
  4. Control.VisibleChanged
  5. Control.GotFocus
  6. Form.Activated
  7. Form.Shown

關閉表單:

  1. Form.Closing
  2. 表.FormClosing
  3. Form.Closed
  4. Form.FormClosed
  5. Form.Deactivate
  6. Control.LostFocus
  7. Control.HandleDestroyed
  8. Component.Disposed
0

我已經看到了這種行爲,可能有一些與MDI:

form.Focus(); // calls OnHandleCreated (this.Handle is x) 
form.Show(); // calls OnLoad (this.Handle is still x), then OnHandleCreated (this.Handle is y) 
相關問題