null计算机等级考试 二级VB计算机等级考试 二级VB信息工程学院 包怀忠
E-Mail:baohzh@126.com
手机:15055260972目 录目 录CH11 键盘与鼠标事件过程1CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程大纲
1、KeyPress事件。
2、KeyDown与KeyUp事件。
3、鼠标事件。
4、鼠标光标。
5、拖放。 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程重点
1.KeyPress事件
格式
Sub object_KeyPress(KeyAscii As Integer)
事件响应
焦点在对象上,按下ASCII码键激活事件
KeyAscii参数
返回用户按键的ASCII码;
在事件中修改该参数的值将修改用户输入的ASCII字符,如将之设为0将取消用户输入。CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程2.KeyDown与KeyUp事件
格式
Sub object_KeyDown(keycode As Integer, shift As Integer)
Sub object_KeyUp(keycode As Integer,shift As Integer)
事件响应:用户按下或弹起任意键
Keycode:键盘扫描码(下档字符、大写字符的Ascii码)
Shift:返回用户按键的同时是否按下Shift(1)、Ctrl(2)或Alt(4)键,也可以是这三个键的组合。
CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程窗体的KeyPreview属性
如果将窗体的KeyPreview属性设置为真,则首先触发窗体的KeyPress、KeyDown、KeyUp事件,然后触发焦点所在控件的相关事件。CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程3. 鼠标事件
格式
Sub Object_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Sub Object_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Sub Object_MouseMove(Button As Integer, Shift As Integer,X As Single, Y As Single)
说明
Button:左键(1)、右键(2)、中间(3)
Shift:同键盘事件
X,Y:鼠标光标位置CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程4.鼠标光标(*)
MousePoiner属性
可以在代码中或属性窗口设置,用以改变鼠标光标。
自选鼠标光标
将MousePointer设置为自定义(99)
设置MouseIcon属性为指定光标文件。
Form1.MousePointer=99
Form1.MouseIcon=LoadPicture(“C:\ab.ico”)CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程5.拖放
属性
DragMode
0:手工拖放
1:自动拖放,这时对象将不再响应Click和MouseDown事件。
DragIcon
用于加载对象拖放时显示的图标,可以通过LoadPicture加载CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程事件
Private Sub <对象名>_DragDrop(Source As Control, X As Single, Y As Single)
Private Sub <对象名>_ DragOver(Source As Control, X As Single, Y As Single, State As Integer)
事件响应:
当拖动一个源对象到目标对象并松开鼠标后,目标对象响应DragDrop事件。
当拖动一个源对象穿越目标对象时,目标对象响应DragOver事件。
Source:被拖放的对象
X,Y:鼠标光标位置
State:0(鼠标光标正进入对象区域)、1(鼠标光标正退出对象区域)、2(鼠标光标正位于对象区域之内)CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程方法
Move
Drag
功能:用于启动或终止一个拖放过程
格式:<对象.>Drag
n是一个整数
0:取消拖放
1:开始拖放
2:结束拖放CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程自动拖放
将源对象的DragMode属性设置为1
在目标对象的DragDrop事件中调用Move方法移动该对象到目标对象上。Source.Move X, Y
手工拖放
在源对象MouseDown事件内启动拖放
<源对象>.Drag 1
在源对象MouseUP事件内结束拖放
<源对象>.Drag 2
在目标对象的DragDrop事件中调用Move方法移动该对象到目标对象上。Source.Move X, YCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程考试
选择题(4分)
1.把窗体的KeyPreview属性设置为True,然后编写如下事件过程
Private Sub Form_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr(KeyAscii)
KeyAscii = Asc(UCase(ch))
Print Chr(KeyAscii + 2)
End Sub
程序运行后,按键盘上的“A”键,则在窗体上显示的内容是( )。
A.A B.B C.C D.D CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程2.在窗体上画一个名称为Command1的命令按钮,然后编写如下程序:
Dim SW As Boolean
Function func(X As Integer) As Integer
If X < 20 Then Y = X Else Y = 20 + X
func = Y
End Function
Private Sub Command1_Click()
Dim intNum As Integer
intNum = InputBox("")
If SW Then Print func(intNum)
End SubCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
SW = False
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
SW = True
End Sub
程序运行后,单击命令按钮,将显示一个输入对话框,如果在输入对话框中输入25,则程序的执行结果为( )。
A 输出0 B 输出25 C 输出45 D 无任何输出 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程3.以下关于KeyPress事件过程中参数KeyAscii的叙述中正确的是( )。
A)KeyAscii参数是所按键的ASCII码
B)KeyAscii参数的数据类型为字符串
C)KeyAscii参数可以省略
D)KeyAscii参数是所按键上标注的字符 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程4.设窗体上有一个名为Text1的文本框,并编写如下程序:
Private Sub Form_Load()
Show : Text1.Text = "“ : Text1.SetFocus
End Sub
Private Sub Form_MouseUp(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Print "程序设计"
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Print "Visual Basic";
End Sub
程序运行后,如果在文本框中输入字母“a”,然后单击窗体,则在窗体上显示的内容是
A)Visual Basic B)程序设计
C)Visual Basic程序设计 D)a程序设计 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程5.在窗体上画1个文本框,其名称为Text1,然后编写如下过程:
Private Sub Text1_KeyDown(KeyCode As Integer,Shift As Integer)
Print Chr(KeyCode);
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer,Shift As Integer)
Print Chr(KeyCode+2)
End Sub
程序运行后,把焦点移到文本框中,此时如果敲击“A”键,则输出结果为。
A)AA B)AB C)AC D)ADCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程6.在窗体上画1个命令按钮,名称为Command1,然后编写如下程序:
Dim Flag As Boolean
Private Sub Command1_Click()
Dim intNum As Integer
intNum=InputBox("请输入:")
If Flag Then Print f(intNum)
End Sub
Function f(X As Integer)As Integer
If X<10 Then Y=X Else Y=X+10
f=Y
End FunctionCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程Private Sub Form_MouseUp(Button As Integer,Shift As Integer,X As Single,Y As Single)
Flag=True
End Sub
运行程序,首先单击窗体,然后单击命令按钮,在输入对话框中输入5,则程序的输出结果为。
A) 0 B) 5 C) 15 D) 无任何输出 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程7.在窗体上画一个名称为 TxtA 的文本框,然后编写如下的事件过程:
Private Sub TxtA_KeyPress(KeyAscii As Integer)
……
End Sub
假定焦点已经位于文本框中,则能够触发 KeyPress 事件的操作是
A.单击鼠标 B.双击文本框
C.鼠标滑过文本框 D.按下键盘上的某个键 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程8.在窗体上画一个命令按钮和两上文体,其名称分别为 Command1 、 Text1 和 Text2 ,然后编写如下程序:
Dim S1 As String, S2 As String
Private Sub Form_Load()
Text1. Text=”” :Text2. Text=””
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
S2=s2 & Chr(KeyCode)
End Sub CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程Private Sub Text1_KeyPress(KeyAscii As Integer)
S1=S1 & chr(KeyAscii)
End Sub
Private Sub Command1_Click()
Text1.Text=S2 :Text2.Text=S1
S1= “” :S2= “”
End Sub
程序运行后,在 Text1 中输入“ abc ”,然后单击命令按钮,在文本框 Text1 和 Text2 中显示的内容分别为
A.abc 和 ABC B .abc 和 abc
C.ABC 和 abc D .ABC 和 ABC CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程9.在窗体上先后画2个图片框,名称分别为Picture1和 banana,banana中添加了香蕉图片,且将 banana.DragMode属性设置为1。要求程序运行时,可以用鼠标把banana拖拽到Picture1中。能实现此功能的事件过程是 ()
A) Private Sub Form_DragDrop(Source As Control,X As Single,Y As Single)
banana.Move Picture1.Left+X,Picture1.Top+Y
End Sub CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程B)Private Sub banana_DragDrop(Source As Control,X As Single,Y As Single )
Source.Move Picture1.Left+X,Picture1.Top+Y
End Sub
C) Private Sub Picture1_DragDrop(Source As Control,X As Single,Y As Single )
Source.Move Picture1.Left+X,Picture1.Top+Y
End Sub
D) Private Sub Picture1_DragDrop(Source As Control,X As Single,Y As Single )
banana.Move banana.Left+X, banana.Top+Y
End Sub CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程10.在窗体上从左到右有Text1、Text2两个文本框,要求程序 运行时,在Text1中输入1个分数后按回车键,则判断分数的合法性,若分数为0~100中的1个数,则光标移到Text2中;否则光标不动,并弹出对话框显示“分数错”,下面程序中正确的是()CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程A)
Private Sub Text1_KeyPress(KeyAscii AS Integer)
If KeyAscii=13 Then ‘回车符的ACSII码是13
a=Val(Text1)
If a>=0 or a<=100 Then
Text2.SetFocus
Else
Text1. SetFocus : MsgBox(“分数错”)
End If
End If
End SubCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程B)
Private Sub Text1__KeyPress(KeyAscii AS Integer)
If KeyAscii=13 Then ‘回车符的ACSII码是13
a=Val(Text1)
If a>=0 And a<=100 Then
Text1.SetFocus
Else
Text2. SetFocus : MsgBox(“分数错”)
End If
End If
End SubCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程C)
Private Sub Text1__KeyPress(KeyAscii AS Integer)
If KeyAscii=13 Then ‘回车符的ACSII码是13
a=Val(Text1)
If a<0 And a>100 Then
Text2.SetFocus
Else
Text1. SetFocus : MsgBox(“分数错”)
End If
End If
End SubCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程D)
Private Sub Text1_KeyPress(KeyAscii AS Integer)
If KeyAscii=13 Then ‘回车符的ACSII码是13
a=Val(Text1)
If a>=0 And a<=100 Then
Text2.SetFocus
Else
Text1. SetFocus : MsgBox(“分数错”)
End If
End If
End SubCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程11.有一个名称为Form1的窗体,上面没有控件,设有以下程序(方法Pset(X,Y)的功能是在坐标X,Y处画一个点):
Dim cmdmave As Boolean
Private Sub Form_MouseDown(Button As Integer,Shift As Integer, X As Single,Y As Single)
cmdmave = True
End Sub
Private Sub Form_MouseMove(Button As Integer,Shift As Integer, X As Single, Y As Single)
If cmdmave Then Form1.Pset(X,Y)
End SubCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single,Y As Single)
cmdmave = False
End Sub
此程序的功能是
A)每按下鼠标键一次,在鼠标所指位置画一个点
B)按下鼠标键,则在鼠标所指位置画一个点;放开鼠标键,则此点消失
C)不按鼠标键而拖动鼠标,则沿鼠标拖动的轨迹画一条线
D)按下鼠标键并拖动鼠标,则沿鼠标拖动的轨迹画一条线,放开鼠标键则结束画线 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程12.文本框Text1的KeyDown事件过程如下
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
End Sub
其参数KeyCode的值示的是发生此事件时
A)是否按下Alt键或Ctrl键
B)按下的是哪个数字键
C)所按的键盘键的键码
D)按下的是哪个鼠标键CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程13.窗体上有两个名称为Text1、Text2的文本框。Text1的KeyUp事件过程如下:
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim c As String
c = UCase(Chr(KeyCode))
Text2.Text = Chr(Asc(c) + 2)
End Sub
当向文本框Text1中输入小写字母a时,文本框Text2中显示的是
A)A B)a C)C D)cCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程填空题(2分)
1.在窗体上画1个命令按钮和1个文本框,其名称分别为Command1和Text1,然后编写如下代码:
Dim SaveAll As String
Private Sub Command1_Click()
Text1.Text=Left(UCase(SaveAll),4)
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
SaveAll=SaveAll+Chr(KeyAscii)
End Sub
程序运行后,在文本框中输入abcdefg,单击命令按钮,则文本框中显示的内容是【 】。 ABCDCH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程2.在窗体上画一个名称为 Combo1 的组合框,然后画两个名称分别为 Label1 、 Label2 ,标题分别为 " 城市名称 " 和空白的标签。程序运行后,在组合框中输入一个新项目并按回车键,如果输入的项目在组合框的列表中不存在,则自动将其添加到组合框的列表中,并在 Label2 中给出提示 " 已成功添加新输入页。 " 。如果输入的项目已存在,则在 Label2 中给出提示 " 输入项已在组合框中。 " 。请填空。 CH11 键盘与鼠标事件过程CH11 键盘与鼠标事件过程Private Sub Combol_KeyPress(KeyAscii As Integer)
If KeyAscii=13 Then
For i=0 To Combo1.ListCount-1
If Combo1.Text= 【 】 Then
Label2.Caption=" 输入项已在组合框中。 "
Exit Sub
End If
Next i
Label2.Caption=" 已成功添加新输入项。 "
Combo1.【 】 Combo1.Text
End If
End Sub Combo1.List(i) AddItemnull