学号: 姓名: 班级: 实验时间: 实验编号 VB05 实验名称 常用控件
实验目的和要求 1. 掌握常用控件的属性和方法
实验内容 1. 单选按钮的使用; 2. 复选框的使用;
3. 框架的使用;
4. 列表框的使用。
一、实验题目
1. 设计一程序,如下图所示:要求,当单击“确定”按钮后,文本框里的文字会按所选择的单选按钮或复选框内容进行设置。
图5-1 单选按钮和复选框
(1)代码编写Private Sub Command1_Click()
Text1.FontName = "宋体"
If Check1.Value = 1 Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
If Check2.Value = 1 Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
Visual Basic 程序设计 实验(训)报告
End If
If Check3.Value = 1 Then Text1.FontStrikethru = True Else
Text1.FontStrikethru = False End If
If Check4.Value = 1 Then Text1.FontUnderline = True Else
Text1.FontUnderline = False End If
Text1.FontName = "黑体"
If Check1.Value = 1 Then Text1.FontBold = True
Else
Text1.FontBold = False
End If
If Check2.Value = 1 Then Text1.FontItalic = True
Else
Text1.FontItalic = False End If
If Check3.Value = 1 Then Text1.FontStrikethru = True Else
Text1.FontStrikethru = False End If
If Check4.Value = 1 Then Text1.FontUnderline = True Else
Text1.FontUnderline = False End If
End Sub
(2)运行结果截图
2. 如下图所示,要求当选择了两个框架内的单选按钮后,单击“确定”,会在文本框里显示“您选择的x体y色”。(x为宋体或黑体;y为红色或绿色)
图5-2 单选按钮与框架
(1)代码编写Private Sub Command1_Click()
If Option1.Value = True And Option3.Value = True Then
Text1.Text = "您选择了宋体红色"
Text1.FontName = "宋体"
Text1.ForeColor = vbRed
End If
If Option1.Value = True And Option4.Value = True Then
Text1.Text = "您选择了宋体绿色"
Text1.FontName = "宋体"
Text1.ForeColor = vbGreen
End If
If Option2.Value = True And Option3.Value = True Then
Text1.Text = "您选择了黑体红色"
Text1.FontName = "黑体"
Text1.ForeColor = vbRed
End If
If Option2.Value = True And Option4.Value = True Then
Text1.Text = "您选择了黑体绿色"
Text1.FontName = "黑体"
Text1.ForeColor = vbGreen
End If
End Sub
(2)运行结果截图
3. 在窗体上画两个列表框,名称分别为List1和List2,在List2中预设内容为:“学号”,“姓名”“性别”“年龄”“班号”“照片”“简历”;还有两个命令按钮,名称分别为C1、C2,标题分别为“添加”、“清除”,如图所示。程序的功能是:在运行时,如果选中右边列表框(List2)中的一个列表项,单击“添加”按钮,则把该项移到左边的列表框中;若选中左边列表框(List1)中的一个列表项,单击“清除”按钮,则把该项移动到右边的列表框中。
图5-3 列表框
(1)代码编写
(2)运行结果截图
4. 在名称为Form1的窗体上画一个名称为List1的列表框,并任意输入若干列表项;再画一个
名称为Text1的文本框,无初始内容。请编写List1和Text1的Click事件过程。程序运行后,如果单击列表框中的某一项,则在文本框中显示该项相应的顺序号,即:若单击第一项,则在文本框中显示1,若单击第二项,则在文本框中显示2(如图所示);如果单击文本框,则把该列表项的内容显示在文本框中。
(1)代码编写
(2)运行结果截图