过程和函数练习
窗体一模块:
Public Class Form1
Public Sub delayloop(ByVal delaytime As Integer)
Dim loopfinish As Integer
Const secondsinday = 24& * 60& * 60&
loopfinish = Second(Now) + delaytime
If loopfinish > secondsinday Then
loopfinish = loopfinish - secondsinday
Do While Second(Now) > loopfinish
Loop
End If
Do While Second(Now) < loopfinish
Loop
End Sub
Sub a(ByVal c As Integer, ByVal k As Integer, ByRef s As Integer) 'byref 指定参数的引用,通过引用参数返回值 (通用过程不能直接返回值)
c = Val(InputBox("请输入矩形的长", "输入", 0))
k = Val(InputBox("请输入矩形的宽", "输入", 0))
s = c * k
End Sub
Function ss(ByVal c2 As Integer, ByVal k2 As Integer) As Integer '函数总会返回值
Dim s3 As Integer
s3 = c2 * k2
Return s3
'ss = c2 * k2 的结果通上
End Function
Sub d()
MsgBox("第一个窗体的模块的过程")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim c1, k1, s1, s5, c3, k3 As Integer
Call a(c1, k1, s1)
MsgBox("调用过程得到矩形的面积为" & Str(s1))
c3 = Val(InputBox("请输入矩形的长", "请输入", 0))
k3 = Val(InputBox("请输入矩形的宽", "请输入", 0))
s5 = ss(c3, k3)
MsgBox("通过调用函数求得矩形的面积为:" & Str(s5))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n2 As New Form2 ' 多窗体程序设计 建立窗体类的实例
n2.d()
n2.Show()
End Sub
End Class
窗体二模块:
Public Class Form2
Sub d()
MsgBox("第二个窗体模块的过程。")
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim n1 As New Form1
n1.d()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'产生一定范围内的随机整数
'公式为 int((上限值(大) - 下限值(小) + 1) * rnd()) + 下限值
Dim i As Short
For i = 1 To 1000
Debug.WriteLine(Str(Int((13 - 1 + 1) * Rnd()) + 1)) '产生1`~13之间的随即整数
Next
End Sub
End Class