當(dāng) Windows Form 應(yīng)用程序啟動時,會以下列順序引發(fā)主要表單的啟動事件:
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
當(dāng)應(yīng)用程序關(guān)閉時,會以下列順序引發(fā)主要表單的關(guān)閉事件:
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.companysz.com/Interkey/p/3679288.html
在窗體關(guān)閉時,F(xiàn)ormClosing 事件發(fā)生。窗體關(guān)閉時,此事件會得到處理,從而釋放與此窗體關(guān)聯(lián)的所有資源。如果取消此事件,則該窗體保持打開狀態(tài)。若要取消窗體的關(guān)閉操作,請將傳遞給事件處理程序的 FormClosingEventArgs 的 Cancel 屬性設(shè)置為 true。
當(dāng)窗體顯示為模式對話框時,單擊“關(guān)閉”按鈕(窗體右上角帶 X 的按鈕)會隱藏窗體并將 DialogResult 屬性設(shè)置為 DialogResult.Cancel。通過在窗體的 FormClosing 事件的事件處理程序中設(shè)置 DialogResult 屬性,可以在用戶單擊“關(guān)閉”按鈕時重寫分配給 DialogResult 屬性的值。
在用戶或 application 類的 Close 方法或 Exit 方法關(guān)閉窗體后,會發(fā)生 FormClosed 事件。若要防止窗體關(guān)閉,請?zhí)幚?FormClosing 事件,并將傳遞給事件處理程序的 CancelEventArgs 的 Cancel 屬性設(shè)置為 true。
可以使用此事件執(zhí)行一些任務(wù),如釋放窗體使用的資源,還可使用此事件保存輸入窗體中的信息或更新其父窗體。
如果窗體是多文檔界面 (MDI) 父窗體,則在引發(fā) MDI 父窗體的 FormClosing 事件之前將引發(fā)所有 MDI 子窗體的 FormClosing 事件。同樣,在引發(fā) MDI 父窗體的 FormClosed 事件之前,將引發(fā)所有 MDI 子窗體的 FormClosed 事件。
歡迎加群:.NET反編譯|破解 群號:183569712(請輸入驗(yàn)證信息:博客園).
參考:
1、C#中WinForm窗體事件的執(zhí)行次序
2、窗體的關(guān)閉事件
新聞熱點(diǎn)
疑難解答
圖片精選