Print\通用过程s1中的变量:\End Sub
【例6-13】
Dim a As Integer,b As Integer '声明模块级变量
Private Sub Command1_Click()
A = 100:b = 8 '对模块级变量赋值 Print
Print\调用s1前,模块级变量:\ Call s1 '调用通用过程sub1 Print
Print\调用s1后,模块级变量:\End Sub
Sub s1() '通用过程
A = 55:b = 66 '对模块级变量赋值 Print
Print\通用过程s1对模块级变量赋值:\End Sub 【例6-14】
(4)在窗体Form1的代码窗口的顶部,声明模块级变量a和b,分别用来储存程序运行后单击左右两个命令按钮的次数。代码如下:
Private a As Integer Private b As Integer
(5)编写左边的命令按钮的Command1_Clic事件过程。代码如下: Private Sub Command1_Click() Dim s As String I = I + 1 A = a + 1
S = \单击按钮\次,左按钮\次\ MsgBox s,vbOKOnly,\提示\End Sub
(6)编写右边的命令按钮的Command2_Clic事件过程。代码如下: Private Sub Command2_Click() Dim s As String I = I + 1 B = b + 1
S = \单击按钮\次,右按钮\次\ MsgBox s,vbOKOnly,\提示\End Sub 【例6-15】
Sub change()
16
Dim d As Integer '声明动态变量d Static s As Integer '声明静态变量s D = d + 1 S = s + 1
Print \动态变量d = \静态变量s = \End Sub
Private Sub Command1_Click() Dim i As Integer For i = 1 To 3
change '或 Call change Next i End Sub 实训
(3)在Form1的窗体模块的代码窗口的最顶部(通用声明段)声明模块级变量title,用来存储字符串。代码如下:
Private title As String '表明是使用通用过程还是函数
(4)定义Sub通用过程MySub,来进行乘法运算。其中的形参x、y按值传递,形参z按地址传递。代码如下:
Private Sub MySub(ByVal x As Integer,ByVal y As Integer,z As Integer) Z = x * y End Sub
定义函数过程MyFun,来进行加法运算。其中的形参m和n是按值传递的。代码如下: Private Function MyFun(ByVal m As Integer,ByVal n As Integer) As Integer MyFun=m+n End Function
(5)添加窗体的事件过程Form_Load,来做些初始化的工作,将文本框置空。代码如下: Private Sub Form_Load() Text1.Text = \ Text2.Text = \ Text3.Text = \End Sub
分别添加两个单选按钮的Click事件过程,来设置框架Frame1和标签Label的Caption属性。代码如下:
Private Sub Option1_Click()
Frame1.Caption = \乘法积运算\ Label1.Caption = \\End Sub
Private Sub Option2_Click() Frame1.Caption = \加法运算\ Label1.Caption = \End Sub
添加命令按钮的事件过程Command1_Click。其中,关键字static声明的静态变量i和j用来储存运算的次数,关键字Dim声明的过程级局部变量a、b、c用来储存三个文本框里的
17
数值。代码如下:
Private Sub Command1_Click() Static i As Integer,j As Integer
Dim a As Integer,b As Integer,c As Integer A = Val(Text1.Text) B = Val(Text2.Text)
If Option1.Value=True Then Title = \用过程运算\ Call MySub(a,b,c) i=i+1
Form1.Caption = \第\次\ Else
Title = \用函数运算\ c=MyFun(a, b) j = j+1
Form1.Caption = \第\次\ End If
Text3.Text = c End Sub 第7章 课前体验
Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, _ X As Single, Y As Single) Form1.Caption = \测试鼠标事件\ Select Case Button
Case 1 '左键,或用常量Visual BasicLeftButton FrameLeft.BackColor = Visual BasicRed FrameRight.BackColor = Visual BasicWhite
Frame1.ToolTipText = \朋友,您刚才在这按了鼠标左键!\ Case 2 '右键 ,或用常量Visual BasicRightButton FrameRight.BackColor = Visual BasicRed FrameLeft.BackColor = Visual BasicWhite
Frame1.ToolTipText = \朋友,您刚才在这按了鼠标右键!\ End Select End Sub 【例7-1】
Private Sub Form_MouseDown(Button As Integer,Shift As Integer,_ X As Single,Y As Single) Print\您触发了MouseDown事件!\End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer,_ X As Single,Y As Single) Print\您又触发了MouseUp事件!\End Sub
18
【例7-2】
Private Sub Form_MouseMove(Button As Integer,Shift As Integer,_ X As Single,Y As Single) If Shift=1 And Button=1Then Cls
Print\鼠标指针当前横坐标X=\ Else Cls
Print\鼠标指针当前纵坐标Y=\ End If End Sub 【例7-3】
1)首先,在窗体模块的顶部声明一个逻辑变量paint,如下: Private paint As Boolean
(2)定义窗体上的按下鼠标按键的事件过程,使得变量paint的值在按鼠标左键时为true。再定义释放鼠标按键的事件过程,使得变量paint的值为false。代码如下:
Private Sub Form_MouseDown(Button As Integer,Shift As Integer,_ X As Single,Y As Single) If Button=1 Then paint=True End If End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, _ X As Single, Y As Single) paint = False End Sub
(3)定义窗体上的鼠标移动事件过程。
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _ X As Single, Y As Single)
If paint Then ?paint为true时,程序画出轨迹点 PSet(X,Y) End If End Sub 【例7-4】
(2)定义窗体的Form_load事件过程,使列表框中添加几个选项。代码如下: Private Sub Form_Load() List1.AddItem\ List1.AddItem\ List1.AddItem\ List1.AddItem\End Sub
(3)定义列表框的单击事件过程。 Private Sub List1_Click()
19
Form1.MousePointer=List1.ListIndex End Sub 【例7-5】
Private Sub Form_KeyPress(KeyAscii As Integer) If KeyAscii >= 97 And KeyAscii <= 122 Then
Print\小写字母:\,ASCII码:\ End If
If KeyAscii >= 65 And KeyAscii<=90 Then
Print\大写字母:\,ASCII码:\ End If End Sub 【例7-6】
Private Sub Form_KeyDown(KeyCode As Integer,Shift As Integer) Dim color As String color=Chr(KeyCode) Select Case color Case\
Label1.BackColor = Visual BasicWhite Case\
Label1.BackColor=Visual BasicRed Case\
Label1.BackColor=Visual BasicGreen Case \
Label1.BackColor=Visual BasicBlue End Select End Sub 【例7-7】
Private Sub Form_KeyDown(KeyCode As Integer,Shift As Integer) Select Case Shift Case1
Label1.Caption=\你按了【SHIFT】键!\ Case2
Label1.Caption=\你按了【CTRL】键!\ Case4
Label1.Caption=\你按了 【ALT】键!\ Case3
Label1.Caption=\你同时按了SHIFT和【CTRL】键!\ Case5
Label1.Caption=\你同时按了SHIFT和【ALT】键!\ Case6
Label1.Caption=\你同时按了CTRL和【ALT】键!\ Case7
Label1.Caption=\你同时按了SHIFT、CTRL和【ALT】键!\
20