《VB程序设计》实验报告

更新时间:2023-11-14 10:59:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

(此文档为word格式,下载后您可任意编辑修改!)

台州学院

《VB程序设计》实验报告

实验1.1 设计一个程序,程序运行的初始界面如图1-1所示。当单击“显示”按钮时,在文本框中显示“欢迎使用VB!”(图1-1);当单击“清除”按钮时,清除文本框内的文本;当单击“退出”按钮时,结束程序运行。 Private Sub Command1_Click() Text1.Text = \欢迎使用VB!\End Sub

Private Sub Command2_Click() Text1.Text = \End Sub

Private Sub Command3_Click() End End Sub

实验1.2 设计一个程序,程序运行的初始界面如图1-3所示,其中“显示”按钮为不可用。同时要求:

(1)当单击“改变文字”按钮时,在左边标签文字内容改为“你好,张三!”,同时“改变文字”按钮变为不

1 可用(图1-4);

(2)当单击“隐藏”按钮时,将左边标签设为不可见,同时“隐藏”按钮变为不可用,“显示”按钮变为可用(图1-5);

(3)当单击“显示”按钮时,重新显示左边标签,同时“隐藏”按钮变为可用,“显示”

Private Sub Command1_Click() Label1.Caption = \你好,张三!\

Command1.Enabled = False End Sub

Private Sub Command2_Click() Label1.Visible = False Command3.Enabled = True Command2.Enabled = False End Sub

Private Sub Command3_Click() Label1.Visible = True Command2.Enabled = True Command3.Enabled = False

End Sub

实验2.1 设计一个程序,运行时界面如图2-1所示。当单击“左”“右”、、“上”、“下”按钮时,标签分别向左、右、上、下移动。

Private Sub Command1_Click() Label1.Left = Label1.Left - 100 End Sub

Private Sub Command2_Click()

Label1.Left = Label1.Left + 100 End Sub

Private Sub Command3_Click() Label1.Top = Label1.Top - 100 End Sub

Private Sub Command4_Click() Label1.Top = Label1.Top + 100 End Sub

实验2.2 设计一个程序,在文本框中输入一个3位整数,单击按钮后,在标签上输出该数的百位数、十位数和个位数,运行界面如图2-2所示。 Private Sub Command1_Click() Label1.Caption = \百位数:\Left(Text1.Text, 1) + \十位数:\+ Mid(Text1.Text, 2, 1) + \个位数:\End Sub

实验2.3 设计一个程序,实现简单计算的功能、

Private Sub Command1_Click() Dim a As Single, b As Single, c As Single

a = Text1.Text b = Text2.Text c = a + b

Label1.Caption = \ Text3.Text = c End Sub

Private Sub Command2_Click() Dim a As Single, b As Single, c As Single a = Text1.Text b = Text2.Text c = a - b

Label1.Caption = \ Text3.Text = c End Sub

2 Private Sub Command3_Click() Dim a As Single, b As Single, c As Single a = Text1.Text b = Text2.Text c = a * b

Label1.Caption = \ Text3.Text = c End Sub

Private Sub Command4_Click() Dim a As Single, b As Single, c As Single a = Text1.Text b = Text2.Text c = a b

Label1.Caption = \ Text3.Text = c End Sub

Private Sub Command5_Click() Text1.Text = \ Text2.Text = \ Text3.Text = \End Sub

Private Sub Command6_Click() End End Sub

实验2.4 设计一个程序,单击“输入”按钮可弹出一个输入对话框,提示用户输入带区号的电话号码,区号

为4为,号码为8位,比如输入“7”,然后将区号和电话号码在消息对话框中显示出来,运行界面如图2-4所示。 Private Sub Command1_Click() Dim c As String

c = InputBox(\输入带区号的电话号码,区号为4为,号码为8位,比如输入“7”:\

MsgBox (\区号:\+ Chr(13) + Chr(10) + \号码:\Right(c, 8)) End Sub

实验3.1 在两个文本框中输入实数,单击按钮,显示“较小值是xxx”。 Private Sub Command1_Click() Dim a As Single, b As Single, m As Single

a = Text1.Text b = Text2.Text If a < b Then m = a Else m = b End If

MsgBox \较小值是\End Sub

实验3.2 输入三个实数,计算其中的最大值,运行界面如图3-1所示。 采用变量m来存放中间过程和结果

3 Private Sub Command1_Click() Dim a As Single, b As Single, c As Single, m As Single a = Text1.Text b = Text2.Text c = Text3.Text

If a > b Then '将a,b的较大值存入m m = a Else m = b End If

If c > m Then m = c '再将m与c比较,得到最大值 Text4.Text = m End Sub

注:比较a,b部分可以写成单行If结构:

If a > b Then m = a Else m = b

实验3.3 用输入对话框输入x,根据下式计算对应的y,并在窗体上输出y的值。

Private Sub Command1_Click() Dim x As Single, y As Single x = InputBox(\请输入x的值:\ If x > 10 Then y = Sqr(x) + Sin(x) ElseIf x < 10 Then

y = 2 * x ^ 3 + 6 '注意:乘号不能省略 Else y = 0 End If

Print \的值是\End Sub

实验3.4 由键盘输入(1~7)范围内的整数,试编程将其转换为中文表示的星期几,运行界面如图3-2所示。 Private Sub Command1_Click() Dim n As Integer, w As String '注意:w的类型必须为String n = Text1.Text If n = 1 Then w = \星期一\ ElseIf n = 2 Then w = \星期二\ ElseIf n = 3 Then w = \星期三\ ElseIf n = 4 Then w = \星期四\ ElseIf n = 5 Then w = \星期五\ ElseIf n = 6 Then w = \星期六\ ElseIf n = 7 Then w = \星期日\ Else

4 w = \错误的输入\ End If

Text2.Text = w End Sub

实验3.5(选做)由键盘输入3个数,试编程将它们的中间数打印出来 Private Sub Command1_Click() Dim a As Single, b As Single, c As Single, m As Single a = Text1.Text b = Text2.Text c = Text3.Text

If a >= b And b >= c Then m = b

ElseIf a >= c And c >= b Then m = c

ElseIf b >= a And a >= c Then m = a

ElseIf b >= c And c >= a Then m = c

ElseIf c >= a And a >= b Then m = a Else m = b End If

Text4.Text = m End Sub

实验

4.1 编写过程

Commandl_Click,显示对话框输入n(n>0),循环n次,每次随机产生一个两位数,求出其中的偶数之和,并在标签框Label1上显示。 Private Sub Command1_Click() Dim n As Integer, i As Integer, a As Integer, s As Integer Randomize '初始化随机数 n = InputBox(\请输入n:\ For i = 1 To n

a = Int(Rnd * 90) + 10 '随机生成一个两位数存于变量a中 Print a; '显示在屏幕上,以便校对(实际编程可以省略) If a Mod 2 = 0 Then s = s + a '判断是否为偶数 Next i

Label1.Caption = s End Sub

说明:随机生成两位数也可以用 a=(Int(Rnd*9)+1)*10+Int(Rnd*10)

实验

4.2 编写过程

Commandl_Click,用InputBox函数输入10个学生的成绩,统计后依次用标签控件Labell~Labe13显示优秀(85-100)、通过(60-84)和未通过(小于60)的人数。

Private Sub Command1_Click()

5

本文来源:https://www.bwwdw.com/article/kd1v.html

Top