[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
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,它就是:
所以当看到private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
所以下次只要看到一个范例酱子写,只要再手动注册事件即可。
什么?你问我为什么MSDN没有写要注册这字眼,你问我?我怎么可能会知道,我也恨它们为什么不写...
3.在C#的 Snippet 有事件名称的出现,帅!VB没有,逊!
4..除了上述事件的之外,我们也可以使用On系列的方法,来触发事件
启动
OnHandleCreated OnBindingContextChanged OnLoad OnVisibleChanged OnActivated OnShown
关闭
OnClosing OnFormClosing OnClosed OnFormClosed OnDeactivate
使用覆写即可触发 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
|