2.下面是一个添加和删除程序,程序运行界面如图3所示,单击“添加”按钮(Command1)将文本框(Text1)中的内容加到列表框(List1)中的第一项,如果文本框中没有内容,则给出提示“没有内容,不予添加”。单击“删除”按钮(Command2)则将所有选中表项删除。如果没有选择要删除的表项,则给出提示“请选择删除的项目”。
图3 添加和删除程序
Private Sub Form_Load()
Form1.Caption = \添加与删除\ Text1.Text = \
List1.AddItem \ List1.AddItem \
List1.AddItem \ List1.AddItem \End Sub
Private Sub Command2_Click() If (3) Then MsgBox (\请选择删除的项目\ Else
(4) End If End Sub
Private Sub Command1_Click() If (5) Then MsgBox (\没有内容,不予添加\ Else
(6) Text1.Text = \ End If End Sub
3.下列程序能在一定范围内找出所有素数,要求:文本框用来输入所要找的数的范围,且只能按照先在Text1(必须大于1)、后在Text2(必须大于Text1中的数)输入的顺序,按回车键表示输入结束。在输入结束后,才能单击“Command1”(确定)命令按钮,并在窗体上输出该范围内的所有素数。
Private Sub Form_Load() Command1.Enabled = False (7) = False End Sub
41
VB网络课堂:info.zjfc.edu.cn/vbweb
Private Sub Command1_Click()
Dim n As Integer, m As Integer, i As Integer, p As Integer n = Text1.Text: m = Text2.Text For i = n To m: p = 2 Do While p <= Int(Sqr(i))
If i Mod p = 0 Then Exit Do Else p = p + 1 Loop
If p > Int(Sqr(i)) Then (8) Next i
Text1.Text = \ Command1.Enabled = False End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then
If Val(Text1.Text) < 2 Then Text1.Text = \ Else
Text2.Enabled = True: Text1.Enabled = False End If End If End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer) If (10) Then
If Val(Text2.Text) < Val(Text1.Text) Then
Text1.Text = \ Else
(11) End If
Text2.Enabled = False End If End Sub
4.利用1个定时器、1个标签和2个命令按钮制作一个动态秒表。
要求各控件名称取缺省值,控件Command1、Command2标题分别为“开始”、“结束”。运行时,单击“开始”按钮后秒表开始计时,并在标签上显示总秒数;单击“结束”按钮后,计时结束,在窗体上显示出运行的时间(折算成小时、分钟和秒数)。
Dim x As Long
Private Sub Form_Load()
Timer1.Interval = 1000: Timer1.Enabled = False End Sub
Private Sub Command1_Click() Cls x = 0
42
VB网络课堂:info.zjfc.edu.cn/vbweb
(12) End Sub
Private Sub Command2_Click()
Dim h As Integer, m As Integer, s As Integer Timer1.Enabled = False h = (13) m = (14) s = x Mod 3600 Mod 60
Print \运行了\小时\分\秒\End Sub
Private Sub Timer1_Timer() (15) Label1.Caption = x End Sub
5.阅读下列程序说明和程序,在每小题提供的若干可选答案中,挑选一个正确答案。 【程序说明】单击Command1后计算a?x+b?x+c的值并显示,界面设计以及运行时的显示分别如下图左、图右所示。要求在文本框中不可输入英文字母。
2
【程序】
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single, x As Single, y As Single a = Text1(0).Text: b = Text1(1).Text c = Text1(2).Text: x = Text1(3).Text y = a * x * x + b * x + c Label3.Caption = y End Sub
Private Sub Text1_ (16) (Index As Integer, (17) ) Select Case (18) Case \ K = 0
Case (19) K = 0 End Select End Sub
【供选择的答案】
(16) A、keyPress B、change C、keydown D、keyup
(17) A、keyAscii As Integer B、Value C、Text() D、k As Integer
43
VB网络课堂:info.zjfc.edu.cn/vbweb
(18) A、Chr(KeyAscii) B、Chr(K) C、Asc(K) D、Asc(KeyAscii) (19) A、a To z B、'a' To 'z' C、\To \、'a'...'z' 6、阅读下列程序说明和程序,在每小题提供的若干可选答案中,挑选一个正确答案。 【程序说明】设计一个倒计时程序,程序设计的界面如下图所示,要求单击“设置时间”按钮(Command1)时,用InputBox对话框输入倒计时的时间,单击“计时开始”按钮(Command2)后,程序根据输入的倒计时时间开始倒计时,并将时间显示在标签框Label1中,直到“00:00:00”时,停止计时,并使Label1的背景色变为红色。
Dim ss As Integer '存放秒数 Dim mm As Integer '存放分钟数 Dim hh As Integer '存放小时数 Private Sub Form_Load() Timer1.Enabled = False Timer1.Interval = 1000 End Sub
Private Sub Command1_Click() '设置时间按钮 Dim x As String
x = InputBox(\请用00:00:00格式输入开始计时的时间\Label1.Caption = x ss = Val(Right(x, 2)) (20)
hh = Val(Left(x, 2)) End Sub
Private Sub Command2_Click() '计时开始按钮 (21) End Sub
Private Sub Timer1_Timer() (22) If ss < 0 Then mm = mm - 1 ss = 59 End If
44
VB网络课堂:info.zjfc.edu.cn/vbweb