方法一:
可以看到里面除了窗体外还有两个控件,一个Button,一个CheckBox:
选择某个控件或者(Form1 事件),比如选中(form1 事件),就可以在最后一个下拉框选择相应的事件了,其中粗体的表示已经添加的事件:
方法二:
鼠标在已经有的某个控件的事件内部点击一下,然后就可以在方法一的最后一部里面选择了。
方法三:
属性窗口,点击“事件”按钮,下图的红色框位置:
也可以在事件名称后面选择已经有的子过程,但是参数必须和事件的参数一致。
窗体和控件都有的事件:
Click 单击鼠标
DoubleClick 双击鼠标
MouseDown 按下鼠标(不放)
MouseUp 按下的鼠标按键释放开
MouseMove 鼠标移动
MouseHover 鼠标停留
KeyPress 按下键盘上的按键
……
除了以上事件,窗体/控件还有自己独立的事件,例如窗体的事件:
Load 窗体载入时
FormClosing 窗体关闭时
FormClosed 窗体已经关闭
Closed 同 FormClosed,但是已经过时
Closing 同FormClosing,但是已经过时
如下代码:
Private Sub Form1_FormClosing(sender As Object,e As FormClosingEventArgs) Handles Me.FormClosing Console.WriteLine("FormClosing") End Sub Private Sub Form1_FormClosed(sender As Object,e As FormClosedEventArgs) Handles Me.FormClosed Console.WriteLine("FormClosed") End Sub Private Sub Form1_Closed(sender As Object,e As EventArgs) Handles Me.Closed Console.WriteLine("Closed") End Sub Private Sub Form1_Closing(sender As Object,e As CancelEventArgs) Handles Me.Closing Console.WriteLine("Closing") End Sub
Closing
FormClosing
Closed
FormClosed
FormClosing
Closed
FormClosed
在Closing事件发生的时候,可以通过设置e.Cancel=True来阻止关闭窗体:
Private Sub Form1_FormClosing(sender As Object,e As FormClosingEventArgs) Handles Me.FormClosing If MessageBox.Show("确定关闭窗口?","关闭",MessageBoxButtons.YesNo) = DialogResult.No Then e.Cancel = True End If End Sub点击窗体右上角的X,弹出对话框,如果选择否,那么返回窗体,如果选择是,则退出窗体。