轉貼http://www.dotblogs.com.tw/yc421206/archive/2009/07/24/9668.aspx

 

以下文字備份


 

 

 

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

 


關閉時

'關閉事件順序
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
 

 

 

2.有些事件在VS工具找不到,硬是要手動自己加進去。
問題來了該如何加入事件呢?
MSDN都有寫,咱們照抄就好了。(copy是寫程式必備技巧,別傻到一個字一個字打)
 
VB只要將宣告原封不動的複製就能夠使用了,因為VB有 Handles 來幫我們處理,但C#還得先註冊事件;哪時C#也會有 Handles 來幫忙呢?
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closing
'實作內容
End Sub

   

 

 

下列事件註冊是VS沒自動幫我們做,必須要手動加入:
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是委派的傳遞方法
所以下次只要看到一個範例醬子寫,只要再手動註冊事件即可。
什麼?你問我為什麼MSDN沒有寫要註冊這字眼,你問我?我怎麼可能會知道,我也恨它們為什麼不寫...
3.在C#的 Snippet 有事件名稱的出現,帥!VB沒有,遜!

    

 

 

4..除了上述事件的之外,我們也可以使用On系列的方法,來觸發事件
啟動 

 

 

關閉

 使用覆寫即可觸發 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
文章標籤
全站熱搜
創作者介紹
創作者 nineaddnine 的頭像
nineaddnine

nineaddnine的部落格

nineaddnine 發表在 痞客邦 留言(1) 人氣(1,832)