当前位置:文档之家› 《VB程序设计》实验报告

《VB程序设计》实验报告

台州学院《VB程序设计》实验报告实验1.1设计一个程序,程序运行的初始界面如图1-1所示。

当单击“显示”按钮时,在文本框中显示“欢迎使用VB!”(图1-1);当单击“清除”按钮时,清除文本框内的文本;当单击“退出”按钮时,结束程序运行。

Private Sub Command1_Click()Text1.Text = "欢迎使用VB!" End SubPrivate Sub Command2_Click()Text1.Text = ""End SubPrivate Sub Command3_Click()EndEnd Sub实验1.2 设计一个程序,程序运行的初始界面如图1-3所示,其中“显示”按钮为不可用。

同时要求:(1)当单击“改变文字”按钮时,在左边标签文字内容改为“你好,张三!”,同时“改变文字”按钮变为不可用(图1-4);(2)当单击“隐藏”按钮时,将左边标签设为不可见,同时“隐藏”按钮变为不可用,“显示”按钮变为可用(图1-5);(3)当单击“显示”按钮时,重新显示左边标签,同时“隐藏”按钮变为可用,“显示”Private Sub Command1_Click()Label1.Caption = "你好,张三!"Command1.Enabled = False End SubPrivate Sub Command2_Click()Label1.Visible = FalseCommand3.Enabled = TrueCommand2.Enabled = FalseEnd SubPrivate Sub Command3_Click()Label1.Visible = TrueCommand2.Enabled = TrueCommand3.Enabled = FalseEnd Sub实验2.1 设计一个程序,运行时界面如图2-1所示。

当单击“左”、“右”、“上”、“下”按钮时,标签分别向左、右、上、下移动。

Private Sub Command1_Click()Label1.Left = Label1.Left - 100End SubPrivate Sub Command2_Click()Label1.Left = Label1.Left + 100End SubPrivate Sub Command3_Click()Label1.Top = Label1.Top - 100 End SubPrivate 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) + " 个位数:" + Right(Text1.Text, 1)End Sub实验2.3 设计一个程序,实现简单计算的功能、Private Sub Command1_Click()Dim a As Single, b As Single, c As Singlea = Text1.Textb = Text2.Textc = a + bLabel1.Caption = "+"Text3.Text = cEnd SubPrivate Sub Command2_Click()Dim a As Single, b As Single, c As Singlea = Text1.Textb = Text2.Textc = a - bLabel1.Caption = "-"Text3.Text = cEnd SubPrivate Sub Command3_Click()Dim a As Single, b As Single, c As Singlea = Text1.Textb = Text2.Textc = a * bLabel1.Caption = "*"Text3.Text = cEnd SubPrivate Sub Command4_Click()Dim a As Single, b As Single, c As Singlea = Text1.Textb = Text2.Textc = a / bLabel1.Caption = "/"Text3.Text = cEnd SubPrivate Sub Command5_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""End SubPrivate Sub Command6_Click()EndEnd Sub实验2.4 设计一个程序,单击“输入”按钮可弹出一个输入对话框,提示用户输入带区号的电话号码,区号为4为,号码为8位,比如输入“7”,然后将区号和电话号码在消息对话框中显示出来,运行界面如图2-4所示。

Private Sub Command1_Click()Dim c As Stringc = InputBox("输入带区号的电话号码,区号为4为,号码为8位,比如输入“7”:")MsgBox ("区号:" + Left(c, 4) + 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 Singlea = Text1.Textb = Text2.TextIf a < b Thenm = aElsem = bEnd IfMsgBox "较小值是" & mEnd Sub实验3.2 输入三个实数,计算其中的最大值,运行界面如图3-1所示。

采用变量m来存放中间过程和结果Private Sub Command1_Click()Dim a As Single, b As Single, c As Single, m As Singlea = Text1.Textb = Text2.Textc = Text3.TextIf a > b Then '将a,b的较大值存入mm = aElsem = bEnd IfIf c > m Then m = c '再将m与c 比较,得到最大值Text4.Text = mEnd 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 Singlex = InputBox("请输入x的值:",0) If x > 10 Theny = Sqr(x) + Sin(x)ElseIf x < 10 Theny = 2 * x ^ 3 + 6 '注意:乘号不能省略Elsey = 0End IfPrint "y的值是" & yEnd Sub实验3.4 由键盘输入(1~7)范围内的整数,试编程将其转换为中文表示的星期几,运行界面如图3-2所示。

Private Sub Command1_Click()Dim n As Integer, w As String '注意:w的类型必须为Stringn = Text1.TextIf n = 1 Then w = "星期一"ElseIf n = 2 Thenw = "星期二"ElseIf n = 3 Thenw = "星期三"ElseIf n = 4 Thenw = "星期四"ElseIf n = 5 Thenw = "星期五"ElseIf n = 6 Thenw = "星期六"ElseIf n = 7 Thenw = "星期日"Elsew = "错误的输入"End IfText2.Text = wEnd Sub实验3.5(选做)由键盘输入3个数,试编程将它们的中间数打印出来Private Sub Command1_Click()Dim a As Single, b As Single, c As Single, m As Singlea = Text1.Textb = Text2.Textc = Text3.TextIf a >= b And b >= c Thenm = bElseIf a >= c And c >= b Then m = cElseIf b >= a And a >= c Then m = aElseIf b >= c And c >= a Then m = cElseIf c >= a And a >= b Then m = aElsem = bEnd IfText4.Text = mEnd 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 IntegerRandomize '初始化随机数n = InputBox("请输入n:")For i = 1 To na = Int(Rnd * 90) + 10 '随机生成一个两位数存于变量a中Print a; '显示在屏幕上,以便校对(实际编程可以省略)If a Mod 2 = 0 Then s = s + a '判断是否为偶数Next iLabel1.Caption = sEnd Sub说明:随机生成两位数也可以用a=(Int(Rnd*9)+1)*10+Int(Rnd*10) 实验 4.2 编写过程Commandl_Click,用InputBox函数输入10个学生的成绩,统计后依次用标签控件Labell~Labe13显示优秀(85-100)、通过(60-84)和未通过(小于60)的人数。

相关主题