vb实验报告

  • 格式:doc
  • 大小:484.50 KB
  • 文档页数:12

台州学院

《VB程序设计》实验报告

实验七:常用控件

班级:10物理2

学号:

姓名:

实验日期:2011

一、实验目的和要求

1.掌握基本控件的重要属性、事件;

2.熟练掌握在窗体上建立上述控件的操作方法;

3.熟练掌握事件过程代码的编写;

4.初步掌握建立基于图形界面的应用程序的过程。

二、实验主要仪器和设备

计算机一台,Windows XP操作系统,Visual Basic环境。

三、实验内容

实验7.1 设计一个字体修饰的程序,界面如图7-1所示。要求:框架1中有两个复选框,可以选择粗体和斜体对标签中的文字进行修饰;框架2中有两个单选按钮,可以选择宋体或楷体对标签中的文字进行修饰;标签Label1的文字内容为“Visual Basic 程序设计”,宋体,常规,三号;文字对齐方式为居中。

图7-1 字体修饰

实验7.2 设计一个点菜的程序,界面如图7-2所示。要求:框架中的复选框提供可选择的三种套餐,右边的文本框中可以输入数量;输入时文本框只接受数字键;并且只有选取了相应的套餐后才可以进行输入;如果没有选取套餐,那么文本框不能编辑并清空;单击“确定”按钮,统计点餐的金额,并用消息框显示出来;

图7-2 点菜

实验7.3 设计一密码修改程序,要求。

1) 当单击“确定”按钮后,首先比较“用户名”和“原密码”是否一致,若一致且都为“admin”,则继续执行步骤2;否则弹出消息框,提示用户出错,并让用户重新输入。

2) 比较“新密码(N) ”和“新密码(S) ”,若两者一致,弹出密码修改成功消息框,程序终止执行。否则弹出错误消息,并让用户重新输入。 3) 当单击“取消”按钮后,程序终止执行。

图7-3 密码修改

实验7.4(选做)设计一个拨号盘的程序。界面如图7-4所示。要求:命令按钮数组构成数字键,单击数字键按钮,将拨号的内容显示在文本框Text1中;单击“重拨”按钮,再现原来的拨号过程;

(提示:再现过程由定时器实现)定时器的时间间隔为0.5秒;设置文本框最多接受10个字符。

图7-4 拨号盘

四、实验原始纪录

实验7.1

程序的界面设计。

程序代码。

Private Sub Check1_Click()

If Check1.Value = 1 Then

Label1.FontBold = True

Else

Label1.FontBold = False

End If

End Sub

Private Sub Check2_Click()

If Check2.Value = 1 Then

Label1.FontItalic = True

Else

Label1.FontItalic = False

End If

End Sub

Private Sub Option1_Click()

Label1.FontName = Option1.Caption

End Sub

Private Sub Option2_Click()

Label1.FontName = Option2.Caption

End Sub

实验7.2

程序的界面设计。

程序代码。

Private Sub Check1_Click()

If Check1.Value = 1 Then

Text1.Enabled = True

Else

Text1.Enabled = False

Text1.Text = ""

End If

End Sub

Private Sub Check2_Click() If Check2.Value = 1 Then

Text2.Enabled = True

Else

Text2.Enabled = False

Text2.Text = ""

End If

End Sub

Private Sub Check3_Click()

If Check3.Value = 1 Then

Text3.Enabled = True

Else

Text3.Enabled = False

Text3.Text = ""

End If

End Sub

Private Sub Command1_Click()

Dim a As Integer, b As Integer

a = Val(Text1.Text) * 13 + Val(Text2.Text) * 18 + Val(Text3.Text) * 25

b = MsgBox("总金额为" & a & "元", vbInformation, "金额")

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0

End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)

If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0

End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer)

If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0

End Sub

实验7.3

程序的界面设计。

程序代码。

Private Sub Command1_Click()

a = Text1.Text

b = Text2.Text

c = Text3.Text

d = Text4.Text

If (a = "admin") And (b = "admin") Then

If c = d Then

MsgBox "密码修改成功"

Else

MsgBox "密码输入不一致,请重新输入" Text3.Text = ""

Text4.Text = ""

End If

Else

MsgBox "用户名与密码不一致,请重新输入"

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text4.Text = ""

End If

End Sub

Private Sub Command2_Click()

End

End Sub

实验7.4(选做)

程序的界面设计。

程序代码。

Dim b As String, i As Integer, d As String, c As Integer

Private Sub Command1_Click(Index As Integer)

Select Case Index

Case 0

a = Text1.Text & 1 Text1.Text = a

Case 1

a = Text1.Text & 2

Text1.Text = a

Case 2

a = Text1.Text & 3

Text1.Text = a

Case 3

a = Text1.Text & 4

Text1.Text = a

Case 4

a = Text1.Text & 5

Text1.Text = a

Case 5

a = Text1.Text & 6

Text1.Text = a

Case 6

a = Text1.Text & 7

Text1.Text = a

Case 7

a = Text1.Text & 8

Text1.Text = a

Case 8

a = Text1.Text & 9

Text1.Text = a

Case 9

a = Text1.Text & 0

Text1.Text = a

End Select

End Sub

Private Sub Timer1_Timer()

k = Len(b)

i = i + 1

c = Mid(b, i, 1)

d = d & c

If i Mod k = 0 Then Timer1.Enabled = False

Text1.Text = d

End Sub

Private Sub Command2_Click()

b = Text1.Text

Text1.Text = ""

Timer1.Enabled = True