[VB.NET][C#.NET] Windows Form /控制項 事件 的 先後順序 / 事件方法覆寫

  • 44496
  • 0
  • C#
  • 2009-07-25

[VB.NET][C#.NET] Windows Form /控制項 事件 的 先後順序 / 事件方法覆寫

1.當控制項建立時就會依建立事項一一產生對應的順序

http://msdn.microsoft.com/zh-tw/library/86faxx0d.aspx

啟動時

'啟動事件順序
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

 

2009-7-24 下午 07-57-37

關閉時

'關閉事件順序
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
2009-7-24 下午 08-01-22 

 

  
2009-7-24 下午 08-26-31  
2009-7-24 下午 08-43-10 
  
  
2009-7-24 下午 08-39-28 
2009-7-24 下午 08-47-02  
  
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是委派的傳遞方法
  

  
2009-7-24 下午 09-19-23 

    

 

  

 

關閉

 使用覆寫即可觸發 Form.Load 事件並把原本的 Form.Load 事件給覆寫掉

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
Console.WriteLine(Date.Now & "-----> 3.OnLoad")
End Sub
  
2009-7-24 下午 08-20-30

 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

Image result for microsoft+mvp+logo