vb编程例子
Ⅰ vb编程实例 交互式窗口,手动输入100个数据,求平均值、平方后再开方和自动分类
vb编程实例 交互式窗口,手动输入个数据,求平均值、平方后再开方和自动分类代码如下(附图):
Dim intArray(100) As Single, sglAverage As Single
Private Sub Command1_Click()
Randomize (Timer)
Dim i As Integer, intSum As Integer
Me.Picture1.Cls
For i = 1 To 100
intArray(i) = Int(Rnd(i) * 5 + 95)
Me.Picture1.Print intArray(i);
If i Mod 10 = 0 Then Me.Picture1.Print
intSum = intSum + intArray(i)
Next i
sglAverage = intSum / 100
Label1.Caption = "Sum=" & intSum
Label2.Caption = "Average=" & sglAverage
End Sub
Private Sub Command2_Click()
Dim i As Integer, k0 As Integer, k(4) As Integer
For i = 0 To 4
Me.Picture2(i).Cls
Next i
For i = 1 To 100
If Sqr((intArray(i) - sglAverage) ^ 2) >= 1.67 Then
k(0) = k(0) + 1
Me.Picture2(0).Print intArray(i);
If k(0) Mod 10 = 0 Then Me.Picture2(0).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) >= 1.33 And Sqr((intArray(i) - sglAverage) ^ 2) < 1.67 Then
k(1) = k(1) + 1
Me.Picture2(1).Print intArray(i);
If k(1) Mod 10 = 0 Then Me.Picture2(1).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) >= 1 And Sqr((intArray(i) - sglAverage) ^ 2) < 1.33 Then
k(2) = k(2) + 1
Me.Picture2(2).Print intArray(i);
If k(2) Mod 10 = 0 Then Me.Picture2(2).Print
ElseIf Sqr((intArray(i) - sglAverage) ^ 2) >= 0.67 And Sqr((intArray(i) - sglAverage) ^ 2) < 1 Then
k(3) = k(3) + 1
Me.Picture2(3).Print intArray(i);
If k(3) Mod 10 = 0 Then Me.Picture2(3).Print
Else
k(4) = k(4) + 1
Me.Picture2(4).Print intArray(i);
If k(4) Mod 10 = 0 Then Me.Picture2(4).Print
End If
Next i
For i = 0 To 4
Me.Label8(i).Caption = "共计" & k(i)
Next i
End Sub
Ⅱ 常用vb的编程代码
先在程序中加一个Label,名字应该符合VB程序控件命名规范:
lbl<名称>
再加一个Timer控件,名字也要符合命名规范
在窗体代码对话框中打入
Private Sub Form_Load()
On Error Resume Next
Me.Font = "Arial"
Me.FontSize = 24
Me.FontBold = True
<Timer控件名>.Enabled = True
<Timer控件名>.Interval = 1000
End Sub
'下列函数只能二选其一
'是时间显示
Private Sub <Timer控件名>_Timer()
On Error Resume Next
<Label控件名>.Caption = Time
End Sub
'是日期与时间显示
Private Sub <Timer控件名>_Timer()
On Error Resume Next
<Label控件名>.Caption = Now
End Sub