1、中国石油大学华东 VB练习题(大一下)1、时钟2、滚动条3、列表框4、复选框5、单选框6、组合框7、多分枝选择8、定义函数九、TXT十、数组一、时钟程序运行后,每隔 2 秒命令按钮 Command1 向右移动 100 个单位。Private Sub Form_Load()Timer1.Interval = 2End SubPrivate Sub timer1_timer()Call mymoveEnd SubSub mymove()Command1.Left = Command1.Left + 200End Sub二、滚动条水平滚动条 HScroll1 的数值范围 04000,LargeCha
2、nge 设置为 200,SmallChange设置为 50,水平滚动条 HScroll1 的值(Value)代表命令按钮 Command1 的宽度,当水平滚动条的滑块变化时,命令按钮 Command1 的宽度随之改变Private Sub Form_Load()HScroll1.Min = 0HScroll1.Max = 4000HScroll1.SmallChange = 50HScroll1.LargeChange = 200Command1.Caption = “驻倚靠危楼风细细,无言谁会凭栏意。衣带渐宽终不悔,为伊销得人憔悴“End SubPrivate Sub HScroll1_Ch
3、ange()Command1.Width = Val(HScroll1.Value)End Sub三、列表框 点击“-”按钮,将左边列表中的选中项移动到右边列表的末尾;点击“”按钮,将左边列表所有内容移动到右边列表的末尾;点击“= 0 And i = 0 And i = 0 And i = 0 And i -1 Then _.RemoveItem .ListIndexEnd With End SubPrivate Sub Command5_Click()ClrList List1 List1.ClearEnd SubPrivate Sub Form_Load()List1.Clear: Lis
4、t1.AddItem “语文“:List1.AddItem “数学 “: List1.AddItem “英语“End Sub实现第一条、前一条、后一条、最后一条等 4 按钮的功能 Private Sub form_load()List1.AddItem “财务“List1.AddItem “会计“List1.AddItem “国贸“List1.AddItem “营销“List1.AddItem “工管“Private Sub Command1_Click()List1.Text = List1.List(0)End SubPrivate Sub Command2_Click()List1.Te
5、xt = List1.List(List1.ListIndex - 1)End SubPrivate Sub Command3_Click()List1.Text = List1.List(List1.ListIndex + 1)End SubPrivate Sub Command4_Click()List1.Text = List1.List(List1.ListCount - 1)End Sub4、复选框 窗体中 4 个复选框分别代表 4 各班级,复选框中的标题文字代表班级人数,用户选择复选框后,点击命令按钮 Command1,程序计算出所选班级的总人数,结果放入文本框Text1 中。Pr
6、ivate Sub Command1_Click()Dim s%If Check1.Value = 1 Thens = s + Val(Check1.Caption)End IfIf Check2.Value = 1 Thens = s + Val(Check2.Caption)End IfIf Check3.Value = 1 Thens = s + Val(Check3.Caption)End IfIf Check4.Value = 1 Thens = s + Val(Check4.Caption)End IfText1.Text = Str(s)End SubPrivate Sub Fo
7、rm_Load()Check1.Caption = 30Check2.Caption = 28Check3.Caption = 31Check4.Caption = 29Label1.Caption = “人数“Text1.Text = “End Sub用户选择复选框后,点击命令按钮 Command1,程序根据复选框对文本框Text1 中的字体进行相应的设置。Private Sub Command1_Click()If Check1.Value = 1 Then Text1.FontBold = TrueIf Check2.Value = 1 Then Text1.FontItalic = T
8、rueIf Check3.Value = 1 Then Text1.FontUnderline = TrueEnd Sub五、 单选框用户在数和算法两个框架中各选一个单选按钮,点击命令按钮 Command1,程序根据选中单选按钮进行相应的计算,结果放入文本框 Text1 中。Private Sub Command1_Click()Dim a%, b%, c%, d%k = 1: t = 0a = Val(Option1.Caption)b = Val(Option2.Caption)If Option1.Value And Option3.Value ThenFor i = 1 To ak =
9、 k * iNext iText1.Text = kElseIf Option1.Value And Option4.Value ThenFor i = 1 To at = t + iNext iText1.Text = tElseIf Option2.Value And Option3.Value ThenFor i = 1 To bk = k * iNext iText1.Text = kElseIf Option2.Value And Option4.Value ThenFor i = 1 To bt = t + iNext iText1.Text = tEnd IfEnd Sub5、组
10、合框字体组合框包括宋体、黑体、幼圆、隶书 4 个列表项;字号组合框列表项的值从 12 48,以 4 递增。选择字体组合框或者字号组合框中的某一项,点击按钮 Command1,文本框中的字体和字号进行相应的改变。Private Sub Command1_Click()Text1.FontName = Combo1.TextText1.FontSize = Combo2.TextEnd SubPrivate Sub Form_Load()Combo1.AddItem “宋体“Combo1.AddItem “黑体“Combo1.AddItem “幼圆“Combo1.AddItem “隶书“For i
11、 = 12 To 48 Step 4Combo2.AddItem iNext iText1.Text = “Visual Basic“End Sub七、多分枝加密:点击加密按钮 Command1,输入 1 个小写英文字母,程序将该英文字母变为字母表中其后面的一个字母输出,如 a 改为 b,m 改为 n,z 改为 a,以此类推。点击解密按钮Command2,输入 1 个小写英文字母,程序将其解密后通过输出。使用 InputBox 和 MsgBox 完成输入输出。Private Sub Command1_Click()Dim a As String, b As Stringa = InputBox(“)If Asc(a) = 122 Thenb = Chr(97)Else: b = Chr(Asc(a) + 1)End IfMsgBox bEnd SubPrivate Sub Command2_Click()Dim c As String, d As String