英文VB教程 简体Visual Basic教程 繁体Visual Basic教程

第十四课: VB的用户定义函�



14.1 建立用户定义� VB 函数

函数的一般格式如下:

Public  Function functionName (Arg As dataType,..........) As dataType

Private  Function functionName (Arg As dataType,..........) As dataType

* Public表明该函数是适用于整个程式,

   Private 则表明该函数只适用于某个模块或子程序�
 

范例 14.1

在这个范例中,用户可以计算某数额金钱的未来�(FutureValue� FV), 输出屏幕如图14.1 所示�

�14.1

程序代码

Public Function FV(PV As Variant, i As Variant, n As Variant) As Variant

End Function

Private Sub compute_Click()


Dim FutureVal As Variant
Dim PresentVal As Variant
Dim interest As Variant
Dim period As Variant
PresentVal = TxtPV.Text
interest = TxtRate.Text
period = TxtYears.Text

FutureVal = FV(PresentVal, interest, period)
LblFV.Caption=FutureVal


End Sub

 

 

范例 14.2

以下函数将自动计算学生获得的考试分数和等级,输出屏幕如图14.2 所示�

�14.1

程序代码

Public Function grade(mark As Variant) As String
Select Case mark
Case Is >= 80
grade = "A"
Case Is >= 70
grade = "B"
Case Is >= 60
grade = "C"
Case Is >= 50
grade = "D"
Case Is >= 40
grade = "E"
Case Else
grade = "F"
End Select

End Function

Private Sub compute_Click()

Lbl.Caption = grade(txtMark)
 

End Sub


❮ 上一� 下一� ❯



版权所�©2008 Dr.Liew Voon Kiong。保留所有权� 。联系我�: VB面子�

[Privacy Policy]