[VB.NET][C#.NET] Windows Form /控制項 事件 的 先後順序 / 事件方法覆寫
1.當控制項建立時就會依建立事項一一產生對應的順序
http://msdn.microsoft.com/zh-tw/library/86faxx0d.aspx
啟動時
- Control.HandleCreated
發生於為控制項建立控制代碼時。 Control.BindingContextChanged
發生於 BindingContext 屬性的值變更時。Form.Load
發生在表單第一次顯示之前。Control.VisibleChanged
發生於 Visible 屬性值變更時。Form.Activated
發生於表單以程式碼或由使用者啟動時。Form.Shown
每當第一次顯示表單時發生。
'啟動事件順序
Private Sub Form1_HandleCreated(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.HandleCreated
Console.WriteLine(Date.Now & "-----> 1.Form1_HandleCreated")
End Sub
Private Sub Form1_BindingContextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.BindingContextChanged
Console.WriteLine(Date.Now & "-----> 2.Form1_BindingContextChanged")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Console.WriteLine(Date.Now & "-----> 3.Form1_Load")
End Sub
Private Sub Form1_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
Console.WriteLine(Date.Now & "-----> 4.Form1_VisibleChanged")
End Sub
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Console.WriteLine(Date.Now & "-----> 5.Form1_Activated")
End Sub
Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Console.WriteLine(Date.Now & "-----> 6.Form1_Shown")
End Sub
關閉時
Form.Closing
發生於表單正在關閉時。Form.FormClosing
發生於表單關閉之前。Form.Closed
發生於表單已關閉時。Form.FormClosed
發生於表單關閉之後。Form.Deactivate
發生於表單失去焦點且不再是使用中的表單時。Application 類別的 ApplicationExit 事件會在主要表單的關閉事件之後引發。
'關閉事件順序
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closing
Console.WriteLine(Date.Now & "-----> 1.Form1_Closing")
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Console.WriteLine(Date.Now & "-----> 2.Form1_FormClosing")
End Sub
Private Sub Form1_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Console.WriteLine(Date.Now & "-----> 3.Form1_Closed")
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
Console.WriteLine(Date.Now & "-----> 4.Form1_FormClosed")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
Console.WriteLine(Date.Now & "-----> 5.Form1_Deactivate")
End Sub
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closing
'實作內容
End Sub
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
但,我們怎麼知道該如何註冊事件Handler?要用什麼Handler?要用什麼方法傳遞委派?現在來說明一下,假設在MSDN看到以下說明範例:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//實作內容
}
System.ComponentModel.CancelEventArgs,是傳遞參數,所以我們需要一個事件的Handler,它就是:
System.ComponentModel.CancelEventHandler ,所以我們採用它來委派事件。
Form1_Closing,是方法的名稱,方法傳遞。是跟Handler說要傳遞什麼方法。
所以當看到private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
就表示Form1_Closing是委派的傳遞方法
關閉
使用覆寫即可觸發 Form.Load 事件並把原本的 Form.Load 事件給覆寫掉
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
Console.WriteLine(Date.Now & "-----> 3.OnLoad")
End Sub
5.有幾個已經過時的東西,就儘量鼻要再用它們了吧。(難怪在C#的Snippet 看不到它們倆)
Closing 事件在 .NET Framework 2.0 中已過時,請改用 FormClosing 事件。
Closed 事件在 .NET Framework 2.0 中已過時,請改用 FormClosed 事件。
6.如何將視窗關閉後最小化
利用剛剛所學覆寫 OnFormClosing 方法即可
VB
Protected Overrides Sub OnFormClosing(ByVal e As FormClosingEventArgs)
e.Cancel = True
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
End Sub
C#
protected override void OnFormClosing(FormClosingEventArgs e)//重載關閉函數
{
e.Cancel = true;
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
}
7.範例下載:
CS_EventOder.rar
VB_EventOder.rar
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET