當 Windows Form 應用程序啟動時,會以下列順序引發主要表單的啟動事件:
System.Windows.Forms.Control.HandleCreated
System.Windows.Forms.Control.BindingContextChanged
System.Windows.Forms.Form.Load
System.Windows.Forms.Control.VisibleChanged
System.Windows.Forms.Form.Activated
System.Windows.Forms.Form.Shown
當應用程序關閉時,會以下列順序引發主要表單的關閉事件:
System.Windows.Forms.Form.Closing
System.Windows.Forms.Form.FormClosing
System.Windows.Forms.Form.Closed
System.Windows.Forms.Form.FormClosed
System.Windows.Forms.Form.Deactivate
本文地址:http://www.survivalescaperooms.com/Interkey/p/3679288.html
在窗體關閉時,FormClosing 事件發生。窗體關閉時,此事件會得到處理,從而釋放與此窗體關聯的所有資源。如果取消此事件,則該窗體保持打開狀態。若要取消窗體的關閉操作,請將傳遞給事件處理程序的 FormClosingEventArgs 的 Cancel 屬性設置為 true。
當窗體顯示為模式對話框時,單擊“關閉”按鈕(窗體右上角帶 X 的按鈕)會隱藏窗體并將 DialogResult 屬性設置為 DialogResult.Cancel。通過在窗體的 FormClosing 事件的事件處理程序中設置 DialogResult 屬性,可以在用戶單擊“關閉”按鈕時重寫分配給 DialogResult 屬性的值。
在用戶或 application 類的 Close 方法或 Exit 方法關閉窗體后,會發生 FormClosed 事件。若要防止窗體關閉,請處理 FormClosing 事件,并將傳遞給事件處理程序的 CancelEventArgs 的 Cancel 屬性設置為 true。
可以使用此事件執行一些任務,如釋放窗體使用的資源,還可使用此事件保存輸入窗體中的信息或更新其父窗體。
如果窗體是多文檔界面 (MDI) 父窗體,則在引發 MDI 父窗體的 FormClosing 事件之前將引發所有 MDI 子窗體的 FormClosing 事件。同樣,在引發 MDI 父窗體的 FormClosed 事件之前,將引發所有 MDI 子窗體的 FormClosed 事件。
歡迎加群:.NET反編譯|破解 群號:183569712(請輸入驗證信息:博客園).
參考:
1、C#中WinForm窗體事件的執行次序
2、窗體的關閉事件
新聞熱點
疑難解答