1、1实验二、顺序结构程序设计二、平均成绩Private Sub Command1_Click()Dim a!, b!, c!, d!a =val( Text1.Text)b = val(Text2.Text)c = val(Text3.Text)d = (a + b + c) / 3Text4.Text = str(format(d,0.00)End Sub1、圆球体积Private Sub Command1_Click()Dim r As Single, v As SingleConst pi As Single = 3.141592653r = val(Text1.Text)v = (4 /
2、 3) * pi * r 3Label2.Caption = Format(v, “0.000“)End Sub三、文本编辑编辑按钮Private Sub Command1_Click()Command2.Enabled = TrueText1.Locked = FalseEnd Sub保存按钮Private Sub Command2_Click()Text1.Locked = TrueCommand2.Enabled = FalseEnd Sub四、登录界面登录Private Sub Command1_Click()Text1.Text = “欢迎光临“End Sub退出Private Su
3、b Command2_Click()Text1.Text = “谢谢再见“End Sub五、文本复制选择Private Sub Command1_Click()Text1.SetFocusText1.SelStart = 7Text1.SelLength = 9End Sub复制Private Sub Command2_Click()Label1.Caption = Text1.SelTextEnd Sub提示:圆球体积(V=(4/3) *pi*r) 通过format( )实现数值型数据转换成字符串,并控制小数位数。提示:文本框locked属性,控制文本框是否可以编辑。命令按钮enabled
4、属性控制程序运行时命令按钮是否可用。1、设置文本框TabIndex 为0,可是运行时焦点在文本框中。2、设置命令按钮Default属性为true,用户按enter键会触发该命令按钮的click事件。3、设置命令按钮Cancel 为true 用户按esc 键会触发该命令按钮的click事件。4、通过设置文本框的SelStart 和SelLength 属性可以实现自动选择字符的功能。5、通过引用文本框SelText 属性可以选定文本框内容。2实验三、选择结构程序设计1、整除运算Private Sub Command1_Click()Dim n As Long, r As Stringn = Val
5、(Text1.Text)If (n Mod 3) = 0 And (n Mod 7) = 0 Thenr = “Yes“Else: r = “No“End IfLabel2.Caption = rEnd Sub2、坐标象限Private Sub Command1_Click()Dim x As Single, y As Single, r As Stringx = Val(Text1.Text)y = Val(Text2.Text)If x 0 And y 0 Thenr = “第一象限“ElseIf x 0 Thenr = “第二象限“ElseIf x 0 And y = 285 And d
6、 = 270 And d = 240 And d = 95 Or b = 95 Or c = 95 Thenr = “Honor Medal“Else: r = “No Medal“End IfEnd IfText4.Text = rEnd Sub四、学历认证Private Sub Option1_Click()Text1.Text = “我是博士生“End SubPrivate Sub Option2_Click()Text1.Text = “我是硕士生“End SubPrivate Sub Option3_Click()Text1.Text = “我是本科生“End Sub五、效果设置Pr
7、ivate Sub Check1_Click()If Check1.Value = 1 ThenLabel1.FontUnderline = TrueElse: Label1.FontUnderline = FalseEnd IfEnd SubPrivate Sub Check2_Click()If Check2.Value = 1 ThenLabel1.FontStrikethru = TrueElse: Label1.FontStrikethru = FalseEnd IfEnd Sub提示:1、n能整除3和7条件表达式(n mod 3 =0) and (n mod 7=0)2、若x0且y
8、0,则坐标点位于第一象限3、以复选框的Value 值为条件,用if 语句实现。4、设置标签FontUnderline和FontStrikethru 属性来实现下画线和删除线的效果。3实验四、循环结构程序设计一、整除求和Private Sub Command1_Click()Dim n As Integer, i As Integer, sum As Integer, a As Integern = Text1.Textsum = 0For i = 1 To nIf (i Mod 3 = 0) And (i Mod 7 0) Then sum = sum + iNext iLabel2.Capt
9、ion = sumEnd Sub二、最大公约数Private Sub Command1_Click()Dim m As Integer, n As Integer, i As Integer, r As Integerm = Text1.Textn = Text2.TextIf m n ThenFor i = 1 To nIf (m Mod i = 0) And (n Mod i = 0) Then r = iNext iElseFor i = 1 To mIf (m Mod i = 0) And (n Mod i = 0) Then r = iNext iEnd IfText3.Text =
10、 rEnd Sub三、素数累加Private Sub Command1_Click()Dim m As Integer, n As Integer, sum As Integer, r As Integer, flag As Booleanm = Text1.Textn = Text2.Textsum = 0For i = m To nflag = TrueFor r = 2 To i - 1If (i Mod r) = 0 Thenflag = FalseExit ForEnd IfNext rIf flag = True Then sum = sum + iNext iText3.Text
11、 = sumEnd Sub四、动画设置Private Sub Command1_Click()Timer1.Enabled = TrueLabel1.Left = Label1.Left + 100End SubPrivate Sub Command2_Click()Timer1.Enabled = FalseLabel1.Left = 100End SubPrivate Sub Timer1_Timer()Call Command1_ClickEnd Sub五、求和运算Private Sub Command1_Click()Dim n As Single, i As Single, r As
12、 Single, sum As Singlen = Label1.Captionsum = 0For i = 1 To nFor r = 1 To isum = sum + rNext rNext iText1.Text = sumEnd SubPrivate Sub HScroll1_Scroll()Label1.Caption = HScroll1.ValueEnd Sub4实验五、数组一、筛选元素Dim a(1 To 10) As IntegerPrivate Sub Command1_Click()Text1.Text = “Dim i As IntegerFor i = 1 To 1
13、0a(i) = Int(Rnd * 81 + 10)Text1.Text = Text1.Text & a(i) & Space(2)Next iEnd SubPrivate Sub Command2_Click()Text2.Text = “Dim i As IntegerFor i = 1 To 10If (a(i) Mod 3 = 0) Then Text2.Text = Text2.Text & a(i) & Space(2)Next iEnd Sub二、最大值查找Dim a(1 To 10) As SinglePrivate Sub Command1_Click()Text1.Tex
14、t = “For i = 1 To 10a(i) = Int(Rnd * 26 + 65)Text1.Text = Text1.Text & a(i) & Space(2)Next iEnd SubPrivate Sub Command2_Click()Dim max As Integermax = a(1)For i = 1 To 9If a(i) Val(List2.List(j + 1) Thena = List2.List(j)List2.List(j) = List2.List(j + 1)List2.List(j + 1) = aEnd IfNext jNext iEnd Sub三
15、、随机序列Private Sub Command1_Click()Timer1.Enabled = FalseCommand2.Enabled = TrueCommand1.Enabled = FalseEnd SubPrivate Sub Command2_Click()Timer1.Enabled = TrueCommand1.Enabled = TrueCommand2.Enabled = FalseEnd SubPrivate Sub Command3_Click()List1.RemoveItem List1.ListIndexEnd SubPrivate Sub Timer1_Ti
16、mer()Dim a As Longa = Int(Rnd * 90000 + 10000)List1.AddItem aEnd Sub四、选课统计Private Sub Command1_Click()Dim i As Integer, r As IntegerFor i = 0 To 4If List1.Selected(i) Then r = r + 1Next iText1.Text = rEnd Sub五、球类项目Private Sub Command1_Click()List1.AddItem Combo1.TextEnd SubPrivate Sub List1_DblClick
17、()List1.RemoveItem List1.ListIndexEnd Sub6实验七、过程1、阶乘累加Private Function fact(ByVal n As Integer) As DoubleDim i As Integer, p As Longp = 1For i = 1 To np = p * iNext ifact = pEnd FunctionPrivate Sub Command1_Click()Dim r As Long, j As Integern = Val(Text1.Text)For j = 1 To nr = r + fact(j)Next jText2
18、.Text = rEnd Sub2、降序排序Private Sub Swap(ByRef x%, ByRef y%)Dim t%t = xx = yy = tEnd SubPrivate Sub Command1_Click()Dim a%, b%, c%a = Text1.Textb = Text2.Textc = Text3.TextIf a 0m = nn = rr = m Mod nLoopi = nj = (a * b) / iGcd = jEnd FunctionPrivate Sub Command1_Click()m = Text1.Textn = Text2.TextText
19、3.Text = Gcd(m, n)End Sub4、数列累加Private Function Square(ByVal x%, ByVal n%) As DoubleDim i As Longi = x nSquare = iEnd FunctionPrivate Sub Command1_Click()Dim j As Integer, r As Longx = Text1.Textn = Text2.Textr = 0For j = 0 To nr = r + Square(x, j)Next jText3.Text = rEnd Sub5、显示素数Private Function Is
20、prime(n As Integer) As BooleanDim flag As Boolean, i As IntegerIsprime = TrueFor i = 2 To n - 1If n Mod i = 0 ThenIsprime = FalseExit ForEnd IfNext iEnd FunctionPrivate Sub Command1_Click()Text3.Text = “Dim m As Integer, r As Integer, j As Integerm = Text1.Text: r = Text2.TextFor j = m To rIf Isprim
21、e(j) = True Then Text3.Text = Text3.Text & j & Space(2)Next jEnd Sub7实验八、图形操作一、图片加载Private Sub Command1_Click()Picture1.Picture = LoadPicture(“Wys.bmp“)End SubPrivate Sub Command2_Click()Picture1.Picture = LoadPicture()End Sub二、图片显示Private Sub Check1_Click()If Check1.Value = 0 Then Image1.BorderStyl
22、e = 0If Check1.Value = 1 Then Image1.BorderStyle = 1End SubPrivate Sub Check2_Click()If Check2.Value = 1 Then Image1.Picture = LoadPicture(Pic.jpg)If Check2.Value = 0 Then Image1.Picture = LoadPicture()End Sub三、改变形状Private Sub HScroll1_Scroll()Shape1.Shape = HScroll1.ValueEnd SubPrivate Sub VScroll1
23、_Scroll()Shape1.FillStyle = VScroll1.ValueEnd Sub四、图形绘制Private Sub Command1_Click()Picture1.ClsPicture1.Line (0, 600)-(1600, 600)End SubPrivate Sub Command2_Click()Picture1.ClsPicture1.Circle (800, 600), 500, , , , 2End Sub五、输入控制Private Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii 57 ThenKeyAs
24、cii = 0Label1.Caption = “非法输入“Else: Label1.Caption = “合法输入“End If End Sub8实验九、文件1、满分统计Dim a(1 To 30) As IntegerPrivate Sub Command1_Click()Text1.Text = “Dim i As Integer, j As IntegerOpen “C:UserslenovoDesktopvb91Grade.txt“ For Input As #1i = 1Do While Not EOF(1)Input #1, a(i)i = i + 1LoopClose #1Fo
25、r j = 1 To 30Text1.Text = Text1.Text & a(j) & Space(2)Next jEnd SubPrivate Sub Command2_Click()Dim i As Integer, r As IntegerFor i = 1 To 30If a(i) = 100 Then r = r + 1Next iText2.Text = rEnd Sub2、字符转换Private Sub Command1_Click()Dim s As StringText1.Text = “Open App.Path & “Infile.txt“ For Input As
26、#1Do While Not EOF(1)Line Input #1, sText1.Text = Text1.Text & s & vbCrLfLoopClose #1End SubPrivate Sub Command2_Click()Dim a As Stringa = UCase(Text1.Text)Text1.Text = aOpen App.Path & “Outfile.txt“ For Output As #1Print #1, aClose #1End Sub3、数据选择Dim a(1 To 20) As IntegerPrivate Sub Command1_Click(
27、)Dim i As IntegerText1.Text = “i = 1Open App.Path & “Datain.txt“ For Input As #1Do While Not EOF(1)Input #1, a(i)Text1.Text = Text1.Text & “ “ & a(i)i = i + 1LoopCloseEnd SubPrivate Sub Command2_Click()Dim i As IntegerText2.Text = “For i = 1 To 20If (a(i) Mod 3 = 0) ThenText2.Text = Text2.Text & a(i
28、) & “ “End IfNext iEnd Sub95、文件选择Private Sub Dir1_Change()File1.Path = Dir1.PathEnd SubPrivate Sub Drive1_Change()Dir1.Path = Drive1.DriveEnd SubPrivate Sub File1_Click()Dim a As StringIf Right(File1.Path, 1) = “ Thena = File1.Path + File1.FileNameElsea = File1.Path + “ + File1.FileNameEnd IfText1.T
29、ext = aEnd Sub4、字母统计Private Sub Command1_Click()Dim s As StringText1.Text = “Open App.Path & “Dialog.txt“ For Input As #1Do While Not EOF(1)Line Input #1, sText1.Text = Text1.Text & s & vbCrLfLoopClose #1End SubPrivate Sub Command2_Click()Dim i As Integer, s As String, r As Integerr = 0For i = 1 To Len(Text1.Text)s = Mid(Text1.Text, i, 1)If Asc(s) = 65 And Asc(s) = 97 And Asc(s) = 122 Thenr = r + 1End IfNext iText3.Text = rEnd Sub