《VB语言程序设计》习 题 参 考 答 案第1章习题一、单选题
1.D 2.B 3.C 4.A 5.C 6,A
7.A 8.B 9.C 10.(1)A (2)B
二、多选题
1.ACDE 2.BCDE 3.CD 4,CE
三、填空题
1.事件驱动 2.解释,编译 3.设计,运行,中断
4,属性,方法,事件 5,Command2_Click 6,中央,代码
7.MyForm.frm 8,(1)Cmd1_Click() (2)Print "VB语言程序设计"
编程及上机调试
1,按要求设置窗体属性
2.在窗体上建立一个标签Label1。程序代码如下:
Private Sub Form_Click()
Label1.Caption = "你单击了窗体"
End Sub
Private Sub Form_DblClick()
Label1.Caption = "你双击了窗体"
End Sub
3,在窗体上建立两个文本框Text1(输入数)、Text2(输出数)和一个命令按钮Command1。两个文本框的Text属性均为空。
Private Sub Command1_Click()
Dim x As Single
x = Val(Text1.Text)
Text2.Text = 15 * x
End Sub
第2章习题一、单选题
1.①D,②D 2.①A,②C 3.A 4.B 5.B 6.B
7.C 8.D 9.B 10.B 11.A 12.A
13.B 14.A 15.B 16.A 17.B
二、多选题
1.BD 2.CD 3.AC 4,AD
三、填空题
1.(1) Sin(50*3.14159/180) (2) (2+x*y)/(2–y*y)
(3) a*a–3*a*b/(3+a) (4) x^(3/8)+Sqr(y*y+4*a*a/(x+y^3))
2.Int(50+6*Rnd)
3.(1)138 (2)3 (3)214 (4) "inchina" (5)70 (6) "45"
(7)0
4,"系统管理数据库"
编程及上机调试
1~3题:按要求完成操作练习。
4,Private Sub Command1_Click() '"最大化"按钮
Form1.WindowState = 2 '最大化
End Sub
Private Sub Command2_Click() '"还原"按钮
Form1.WindowState = 0 '还原
End Sub
Private Sub Command3_Click() '"最小化"按钮
Form1.WindowState = 1 '最小化
End Sub
5.在窗体上建立三个文本框Text1(第一个数)、Text2(第二个数)、Text3(计算结果)和一个命令按钮Command1(“计算”)。
Private Sub Command1_Click()
Dim x As Single
Dim y As Single
x = Val(Text1.Text)
y = Val(Text2.Text)
Text3.Text = x * x + y * y
End Sub
6,在窗体上建立两个文本框Text1(输入一串字符)、Text2(输出结果)和一个命令按钮Command1。
Private Sub Command1_Click()
Dim x As String
x = Left(Text1.Text,1) + Right(Text1.Text,1)
Text2.Text = x
End Sub
第3章习题一、单选题
1.C 2.B 3.D 4.D 5.B 6.C
7.B 8.D 9.D 10.A 11.A 12.C
13.C 14.B 15.(1)A (2)C 16.A 17.B
18.(1)D (2)B (3)C 19.D
二、多选题
1.AC 2.AC 3.ABCF
三、填空题
1,$000,124 2.Label1.Caption="a*b=" 3.Height,Width
4.2 5.Text1.SetFocus
6.Multiline,ScrollBars,2 7.编程技术,程序设计
8.Private Sub Text2_Change() ′当文本框Text2中的文本内容发生变动时,执行该事件过程
Label1.Caption=Text1.Text ′把文本框Text1中的文本内容作为标签Label1的标题
End Sub
Private Sub Text1_GotFocus() ′当文本框Text1获得焦点时,执行该事件过程
Text1.SelStart = 0 ′选定文本框Text1中的所有文本内容
Text1.SelLength=Len(Text1.Text)
End Sub
编程及上机调试
1.在窗体上建立一个文本框Text1(输入n值)、三个标签Label1(第一个随机数)、Label2(第二个随机数)和Label3(第三个随机数),以及一个命令按钮Command1。
Private Sub Command1_Click()
n = Val(Text1.Text)
Randomize
x = Int(1 + n * Rnd)
Label1.Caption = "第一个随机数" + Str(x)
x = Int(1 + n * Rnd)
Label2.Caption = "第二个随机数" + Str(x)
x = Int(1 + n * Rnd)
Label3.Caption = "第三个随机数" + Str(x)
End Sub
2.在窗体上建立了四个文本框Text1、Text2、Text3和Text4,以及一个命令按钮Command1(“交换”)和Command2(“合并”)。
Private Sub Command1_Click() '“交换”
t = Text1.Text
Text1.Text = Text2.Text
Text2.Text = Text3.Text
Text3.Text = t
End Sub
Private Sub Command2_Click() '“合并”
t = Text1.Text + Text2.Text + Text3.Text
Text4.Text = t
End Sub
3,(1) Rnd * 9000 + 1000 (2) Str(x) (3) Mid(s,3,1) + Mid(s,2,1) + Left(s,1)
4,Private Sub Command1_Click() '往左
Text1.Left = 0
End Sub
Private Sub Command2_Click() '往右
Text1.Left = Form1.Width - Text1.Width
End Sub
Private Sub Command3_Click() '居中
Text1.Left = (Form1.Width - Text1.Width) / 2
End Sub
5,Private Sub Cmd1_Click()
Lab1.Visible = True
Lab1.Caption = "您已下达显示命令"
End Sub
Private Sub Form_Click()
Cmd1.Visible = True
End Sub
6,Private Sub Form_KeyPress(KeyAscii As Integer)
Show
Print "输入字符:"; Chr(KeyAscii),"ASCII码为;"; KeyAscii
End Sub
Private Sub Form_DblClick()
Cls
End Sub
7,产生这三个消息框的相应语句是:
产生第1个消息框:x = MsgBox("在这里显示提示信息",4 + 0,"请确认")
产生第2个消息框:x = MsgBox("在这里显示" + Chr(13) + "提示信息",3 + 0,"请确认")
产生第3个消息框:x = MsgBox("在这里显示"+Chr(13)+"提示信息",2 + 16 + 0,"请确认")
第4章习题一、单选题
1.C 2.A 3.C 4.C 5,B 6,A
7.D 8.B 9.B 10.(1)D (2)A (3)B
二、多选题
1.ABCFGHJ 2.BE 3.CD 4,CDE 5,AE
三、填空题
1.(S And (A>=18 And A <=20) And H>=1.65) Or (Not S And (A>=16 And Age<=18) And H>=1.60)
2,60000 3,(1) "0" To "9" (2) "a" To "z","A" To "Z" (3) Else
4.(1)6 (2)-2 (3)2
5,Select Case x Case Is < 20
Case Is <= 0 y = 2
y = 4 Case Is <= 30
Case Is < 10 y = 3
y = 0 Case Else
Case Is < 15 y = 4
y = 1 End Select
6.(1) (x>100) And (x Mod 5=0) (2) 0
(3) Len(Text1.Text) (4) Text1.SelText
编程及上机调试
1,在窗体上建立两个文本框Text1(输入数)及Text2(显示结果),一个命令按钮Command1(“判断”)。
Private Sub Command1_Click() '“判断”
x = Val(Text1.Text)
If x Mod 2 = 0 Then
Text2.Text = "偶数"
Else消息
Text2.Text = "奇数"
End If
End Sub
2.窗体上建立了一个文本框Text1,用于显示结果。
Private Sub Form_Load()
x = Val(InputBox("请输入星期几(0~6)"))
Select Case x
Case Is = 0
Text1.Text = "星期日(Sunday)"
Case Is = 1
Text1.Text = "星期一(Monday)"
Case Is = 2
Text1.Text = "星期二(Tuesday)"
Case Is = 3
Text1.Text = "星期三(Wednesday)"
Case Is = 4
Text1.Text = "星期四(Thursday)"
Case Is = 5
Text1.Text = "星期五(Friday)"
Case Is = 6
Text1.Text = "星期六(Saturday)"
Case Else
Text1.Text = "输入错误"
End Select
End Sub
3.窗体上建立了一个文本框Text1(“输入口令”)和一个命令按钮Command1(“检查”)。
Private Sub Command1_Click() '“检查”
x = Text1.Text
If x = "12345678" Then
MsgBox ("欢迎你用机!")
Else
MsgBox ("对不起,口令错")
End If
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text1.MaxLength = 8
Text1.PasswordChar = "*"
End Sub
4.窗体上建立了两个文本框Text1(输入字符串)及Text2(转换结果),一个命令按钮Command1(“清除”)。
Private Sub Command1_Click() '“清除”
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim s As String * 1
s = Chr(KeyAscii)
Select Case s
Case "A" To "Z"
s = LCase(s)
Case "a" To "z"
s = UCase(s)
Case Else
s = "*"
End Select
Text2.Text = Text2.Text + s
End Sub
5,分别选择”红”,”绿”,”蓝”复选框后,单击命令按钮Command1时,通过RGB函数调配的颜色作为一个标签的背景色(BackColor)。
Private Sub Command1_Click()
r = 0,g = 0,b = 0
If Check1.Value = 1 Then '”红”复选框
r = 255
End If
If Check2.Value = 1 Then '”绿”复选框
g = 255
End If
If Check3.Value = 1 Then '”蓝”复选框
b = 255
End If
Label1.BackColor = RGB(r,g,b)
End Sub
6.程序代码如下:
Private Sub Form_Load()
LabClk.Caption = "0"
LabClk.Alignment = 1
LabClk.BorderStyle = 1
LabClk.FontSize = 22
LabClk.FontName = "黑体"
TimClk.Interval = 1000
End Sub
Private Sub TimClk_Timer()
LabClk.Caption = Val(LabClk.Caption) + 1
End Sub
7,(1) Chr(KeyAscii) (2) ElseIf Chr(KeyAscii)
(3) CheC = CheC - 1 (4) CheC (5) Is > 0 或 Else
第5章习题一、单选题
1.A 2,B 3.(1)C,(2)B 4.B 5.C 6.A
7.(1)B,(2)C,(3)B 8,B 9,B 10,B
二、填空题
1.(1)4次,n=13 (2)3次,n=8 (3)3次,n=27 (4)3次,n=10 (5)1次,n=12
2.(1) Len(s) (2) Mid(s,k,1) (3) c + t (4) Text2.Text
3.(1) t = 1 (2) n = n + 1 (3) t = t * 2
4,0 5,Text 6,下拉列表框 7,AddItem,RemoveItem或Clear
8.(1)ItemA (2) ItemD (3) ItemD (4) ItemA
9.(1) Mid(x,p + 3) (2) p + 4 (3) InStr(p,x,"123")
编程及上机调试
1,和数为:1.21。程序代码如下:
Private Sub Form_Load()
Show
s = 0
For k = 1 To 100
s = s + 1 / (1 + k * 4)
Next k
Print s
End Sub
2,程序代码如下:
Private Sub Form_Load()
Dim i As Integer,a As Integer,b As Integer,c As Integer
Show
For i = 100 To 999
a = Int(i / 100)
b = Int((i - 100 * a) / 10)
c = i - 100 * a - 10 * b
If a * a * a + b * b * b + c * c * c = i Then
Print i
End If
Next i
End Sub
3,在窗体上建立一个文本框Text1(初始值)、一个列表框List1(显示结果)和一个命令按钮Comd1(“查找”)。
Private Sub Comd1_Click() '“查找”
Dim n As Integer
n = Val(Text1.Text)
List1.Clear:k = 0
Do While k <= 99
If Not (n Mod 7 = 0) Then
List1.AddItem n
k = k + 1
End If
n = n + 1
Loop
End Sub
4,程序代码如下:
Private Sub Form_Load()
Show
n = Val(InputBox("正整数n="))
For i = 1 To n
For j = i + 1 To n
For k = j + 1 To n
If i * i + j * j = k * k Then
Print "("; i; ","; j; ","; k; ")"
End If
Next k
Next j
Next i
End Sub
5.在窗体上建立一个列表框List1和一个“显示表项”命令按钮Command1。程序代码如下:
Private Sub Command1_click() '"显示表项"按钮单击事件
FontSize = 12
Cls
For i = 0 To List1.ListCount - 1 '逐项判断
If List1.Selected(i) Then '真时为选中
Print List1.List(i)
End If
Next i
End Sub
6.(1) Text1.Text (2) UCase(x) (3) t >= "A" And t <= "Z" (4) y + x
7.程序代码如下:
Private Sub Form_Load()
Show
Dim a As Single,xn0 As Single,xn1 As Single
a = Val(InputBox("请输入一个正数"))
xn1 = a
Do
xn0 = xn1
xn1 = (xn0 + a / xn0) / 2
Loop While Abs(xn0 - xn1) >= 0.00001
Print a; "的平方根为"; xn1
End Sub
第6章习题一、单选题
1,D 2,A 3.(1)B,(2)B 4.C 5,C 6,A
7,(1)C (2)B (3)A (4)B (5)D
二、多选题
1,AD 2,CD
三、填空题
1,二,12; 1,3; -1,2 2,0 1 2 3
2 2 2 2
2 1 0 1
4 4 4 4
3,Name,Index
4,(1) 0 To Index (2)Val(Text1(k).Text)
5,(1) i+1 To k (2) s(i)< s(j) (3) s(i)<>s(i-1) (4) h = h+1
6,(1) ReDim a(1 To n + 1) (2) a(K + 1) = a(K) (3) a(p)
编程及上机调试
1,程序代码如下,
Private Sub Form_Load()
Show
Dim a(100) As Single
n = Val(InputBox("输入个数n"))
Print "处理前数据,"
For k = 1 To n
a(k) = Val(InputBox("请输入第" & k & "个数"))
Print a(k);
Next k
M = Int(n / 2)
For k = 1 To M
h = n - k + 1
t = a(h):a(h) = a(k):a(k) = t
Next k
Print "处理的结果,"
For k = 1 To n
Print a(k);
Next k
End Sub
2,程序代码如下:
Private Sub Form_Load()
Dim a(6,6) As Integer
Show
For i = 1 To 6
For j = 1 To 6
Select Case i
Case Is < j
a(i,j) = 1
Case Is > j
a(i,j) = 2
Case Is = j
a(i,j) = 0
End Select
Print a(i,j);
Next j
Next i
For i = 1 To 6
t = a(1,i):a(1,i) = a(6,i):a(6,i) = t
Next i
Print,Print
For i = 1 To 6
For j = 1 To 6
Print a(i,j);
Next j
Next i
End Sub
3.在窗体上建立两个列表框List1(处理前数据)和List2(处理后数据),以及标签Label1。程序代码如下:
Private Sub Form_Load()
Dim a(100) As Integer,b(100) As Integer
Randomize
n = 100
For i = 1 To n '输入数组a的n个元素
x = Int(20 * Rnd)
List1.AddItem x
a(i) = x
Next i
k = 0
For i = 1 To n '删除零元素
If a(i) <> 0 Then
k = k + 1,b(k) = a(i),List2.AddItem b(k)
End If
Next i
Label1.Caption = "数组中零元素个数:" + Str(n - k)
End Sub
4.(1) g(x - 10) + 1 (2) n - 1 (3) g(t) < g(j)
(4) m = s(t),s(t) = s(k),s(k) = m
5.在窗体上建立4行3列共12个元素的文本框控件数组Text1(0)~Text1(11)
Private Sub Form_Load()
Dim t(12) As String
Randomize
For k = 1 To 12
t(k) = "Empty" '初始安排12个位置均为"Empty"
Next k
For h = 65 To 74 '代表"A"至"J",每次随机安排一个字母
Do While True '随机查找一个空位
p = Int(1 + 12 * Rnd) '产生1~12随机数
If t(p) = "Empty" Then
t(p) = Chr(h)
Exit Do
End If
Loop
Next h
For k = 1 To 12
Text1(k - 1).Text = t(k) '显示座位分配情况
Next k
End Sub
第7章习题一、单选题
1,B 2,C 3.B 4.B 5,C 6,D
7,C 8,B
二、填空题
1,EF 2,7; 1; 4 3,(1)10 (2)28 (3)6 (4) 6 4,Call Comd1_Click
5,(1) n>=16 (2) n\16 (3) Mid(ch,r+1,1)
编程及上机调试
1,在窗体上建立四个文本框Text1和一个命令按钮Command1(“检验”)。文本框Text1~Text3用于输入,Text4用于输出结果。
Private Sub Command1_Click()
Dim a As String,b As String,c As String
a = Trim(Text1.Text)
b = Trim(Text2.Text)
c = Trim(Text3.Text)
If che(a) And che(b) And che(c) Then
Text4.Text = Val(a) + Val(b) + Val(c)
Else
Text4.Text = "存在非数字字符"
End If
End Sub
Function che(x As String) As Boolean '若存在非数字字符,函数值为假
che = True
For k = 1 To Len(x)
s = Mid(x,k,1)
If s < "0" Or s > "9" Then
che = False
Exit For
End If
Next k
End Function
2.在窗体上建立七个文本框Text1~Text7和一个命令按钮Command1(“比较”)。Text1~Text6用于输入,Text7用于输出结果。
Private Sub Command1_Click()
a = Val(Text1.Text):b = Val(Text2.Text):c = Val(Text3.Text)
d = Val(Text4.Text):e = Val(Text5.Text):f = Val(Text6.Text)
Call max(a,b,c,m)
Call max(m,d,e,m)
Call max(m,f,f,m)
Text7.Text = m
End Sub
Sub max(x,y,z,m) ‘标准模块文件
If x > y Then
m = x
Else
m = y
End If
If m < z Then
m = z
End If
End Sub
3,(1) s<>"" (2) Mid(s,p+1) (3) n
4,程序代码如下:
Private Sub Form_Load()
Dim x As Single,n As Integer,y As Double
x = InputBox("x=","求X的n次方")
n = InputBox("n=","求X的n次方")
y = Power(x,n)
Show
Print x; "的"; n; "次方="; Format(y,"###,###.##")
End Sub
Function Power(x As Single,n As Integer) As Double
If n > 1 Then
Power = Power(x,n - 1) * x
Else
Power = x
End If
End Function
5,在窗体上设置三个窗体Form1~Form3。Form1中建立了两个文本框Text1(“用户名”)和Text2(“密码”),以及命令按钮Command1(“判断”)。Form2中建立了一个文本框Text1(其Text属性值为“欢迎你使用本系统”)和命令按钮Command1(“返回”)。Form3中建立了一个文本框Text1(其Text属性值为“对不起,请向管理员查询”)和命令按钮Command1(“退出”)。
Dim n As Integer
Private Sub Command1_Click() ‘窗体Form1的“判断”
usern = Trim(Text1.Text):passw = Trim(Text2.Text)
If usern = "username" And passw = "password" Then
Form1.Hide
Form2.Show
Else
n = n + 1:Text1.SetFocus
If n = 3 Then
Form1.Hide
Form3.Show
End If
End If
Text1.Text = "":Text2.Text = ""
End Sub
Private Sub Form_Activate()
Text1.Text = "":Text2.Text = ""
Text1.SetFocus
End Sub
Private Sub Command1_Click() ‘窗体Form2的“返回”
Form2.Hide
Form1.Show
End Sub
Private Sub Command1_Click() ‘窗体Form3的“退出”
Unload Form1
Unload Form2
Unload Form3
End
End Sub
6,(1) Form1窗体模块程序代码:
Private Sub Form_Load()
Randomize
End Sub
Private Sub Command1_Click() '"生成随机数"
Dim s As String
s = ""
For i = 1 To 10
a(i) = Int(11 + 90 * Rnd)
s = s + Str(a(i)) + ","
Next i
Text1.Text = Left(s,Len(s) - 1)
End Sub
Private Sub Command2_Click() '"求最大数"
Text2.Text = Max()
End Sub
Private Sub Command3_Click() '"打开排序窗口"
Form1.Hide '隐藏主窗体
Form2.Show '显示“排序”窗体
End Sub
Private Sub Command4_Click() '"结束"
Unload Form1 '隐藏主窗体
Unload Form2 '显示“排序”窗体
End
End Sub
(2) Form2窗体模块程序代码:
Private Sub Command1_Click() '“排序”
Dim t As String
Call Sort(t)
Text1.Text = t
End Sub
Private Sub Command2_Click() '“返回”
Form2.Hide '隐藏“排序”窗体
Form1.Show '显示主窗体
End Sub
(3) 标准模块程序代码:
Public a(1 To 10) As Single
Function Max() As Single '求最大值
Dim m As Single
m = a(1)
For i = 2 To 10
If m < a(i) Then
m = a(i)
End If
Next i
Max = m
End Function
Sub Sort(s As String) '排序
s = ""
For i = 1 To 9
For j = i + 1 To 10
If a(i) > a(j) Then
t = a(i),a(i) = a(j),a(j) = t
End If
Next j
s = s + Str(a(i)) + ","
Next i
s = s + Str(a(10))
End Sub
第8章习题一、单选题
1,B 2,D 3.D 4.A 5,B 6,B
7,A 8,(1)D (2)D 9,B
二、填空题
1,随机存取方式 2,Get,Put 3,(1) Open "StData1.txt" For Output As #1
(2) Write #1,StNo,StMb (3) Close #1
4,(1) Open "StData1.txt" For Input As #1 (2) EOF(1)
(3) Input #1,StNo,StMb
5.(1)13 (2)Text1.Text = "9999" (3)Text1.Text
6.(1)num + 1 (2)Input (3)num (4) Stno(num),Text1.Text
(5)Stno(num)
编程及上机调试
1,程序代码如下:
Dim d(1 To 20) As Integer
Private Sub Command1_Click() '"产生随机数"
For i = 1 To 20
d(i) = Int(1 + 99 * Rnd)
List1.AddItem d(i)
Next i
End Sub
Private Sub Command2_Click() '"保存"
Open "MyFile3.txt" For Output As #1
For i = 1 To 20
Write #1,d(i)
Next i
Close #1
End Sub
Private Sub Command3_Click() '"读出"
Open "MyFile3.txt" For Input As #1
Do While Not EOF(1)
Input #1,x
List2.AddItem x
Loop
Close #1
End Sub
Private Sub Command4_Click() '"结束"
End
End Sub
2,本程序的用户界面和程序代码较为复杂,请见具体程序文件。
第9章习题单选题
1.B 2.D 3.D 4.B 5,A 6,A
编程及上机调试
1,程序代码如下:
Private Sub Form_load()
Dim x(3) As Single,y As Single
On Error GoTo errln '以后出错时转移到errln
y = 2 / 0 '产生"除数为0"的错误
x(4) = 1 '产生"下标越界"的错误
MsgBox ("终止程序的运行")
Unload Me '卸载窗体
End
errln,'标号
Select Case Err.Number
Case 11 '"除数为0"的错误码为11
MsgBox ("除数为0(错误码为11),跳过该错误")
Resume Next '返回
Case 9 '"下标越界"的错误码为9
MsgBox ("下标越界(错误码为9),跳过该错误")
Resume Next '返回
Case Else '其他错误处理
MsgBox ("错误发生在" & Err.Source _
& ",代码为" & Err.Number _
& ",即" & Err.Description)
End Select
End Sub
2.改正后的程序代码如下:
Private Sub Form_Load()
Show
For i = 1 To 11
Print Tab(20 - i);
For j = 1 To 2 * i - 1
If j <= i Then
k = j
Else
k = 2 * i - j
End If
If k > 9 Then k = k + 7
Print Chr(k + 48);
Next j
Next i
End Sub
3.(1) n = n + 1 (2) d(1,j) = d(3,j) (3) d(i,2) = d(i,4)
(4) i = j Or i + j = 5 (5) d(i,j) = d(i,j) + 1
(6) n = 0 (7) Text1.Text
第10章习题一、单选题
1,C 2,(1)A (2)C 3.D 4.C 5,A 6,D
7,(1)A (2)D 8,A 9,B 10,B 11,A
12,D
二、填空题
1,(1)Change (2)Value (3)Scroll (4)Value
2,(1) P1.Picture (2) P2.Picture (3) P2.Picture = P3.Picture
3,菜单编辑器,顶;PopUpMenu
4.(1) Button.Index (2) BackColor = RGB(255,0,0)
(3) BackColor = RGB(0,255,0) (4) BackColor = RGB(0,0,255)
5,(1) Picture1 (2) "X=" + Str(X) (3) "Y=" + Str(Y)
6,DragMode,1; 0 7,Action,4,ShowFont
编程及上机调试
1.在窗体上建立一个水平滚动条HScroll1和一个文本框Text1。程序代码如下:
Private Sub Form_Load()
HScroll1.Max = 1000
HScroll1.Min = 0
HScroll1.LargeChange = 5
HScroll1.LargeChange = 10
HScroll1.Value = 500
End Sub
Private Sub HScroll1_Change()
Text1.Text = HScroll1.Value
End Sub
Private Sub HScroll1_Scroll()
Text1.Text = HScroll1.Value
End Sub
2,在窗体上建立一个计时器Timer1和“开始”、“停止”两个命令按钮。程序代码如下:
Private len_S As Single,s As Integer '模块级变量
Private Sub Command1_Click() '“开始”
Timer1.Enabled = True
s = 0
Call Sub_Move
End Sub
Private Sub Command2_Click() '“停止”
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
Timer1.Enabled = False
'先取各指针的长度
len_S = Sqr((LineS.Y2 - LineS.Y1) ^ 2 + (LineS.X2 - LineS.X1) ^ 2) '秒针长度
End Sub
Private Sub Timer1_Timer() '定时器的定时事件过程
s = s + 1
Call Sub_Move
End Sub
Private Sub Sub_Move() '定时器的定时事件过程
pi = 3.14159
LineS.X2 = LineS.X1 + len_S * Sin(pi * s / 30) '绘制秒针
LineS.Y2 = LineS.Y1 - len_S * Cos(pi * s / 30)
End Sub
3,按要求在窗体上设计一个二级下拉菜单。
4,在窗体上建立一个通用对话框CommonDialog1、“打开”及“字体”两个命令按钮和文本框Text1。程序代码如下:
Private Sub Command1_Click() '"打开"
CommonDialog1.DialogTitle = "选择文件"
CommonDialog1.Filter = "文本文件|*.txt" '设置文件过滤器
CommonDialog1.InitDir = "C:\" '设置默认文件夹
CommonDialog1.ShowOpen '显示“打开”对话框
Text1.Text = CommonDialog1.FileName
End Sub
Private Sub Command2_Click() '"字体"
CommonDialog1.Flags = cdlCFScreenFonts
CommonDialog1.ShowFont
Text1.FontName = CommonDialog1.FontName '设置字体名称
Text1.FontSize = CommonDialog1.FontSize '设置字体大小
Text1.FontBold = CommonDialog1.FontBold '粗体
Text1.FontItalic = CommonDialog1.FontItalic '斜体
End Sub
(完)