第10课: VB的内建函数
VB 函数和一般程式非常相似,不过它主要的功能是接受某些数值然后把它交于主程式去完成执行. 函数可分为两种,一种是内建函数,另一种是外部函数。内建函数是VB系统定义的函数,外部函数则是程式师自己设计出来的函数。
在这一课里,我们要学习两个很基本的,但却是很有用的内建函数, 那就是 MsgBox( ) 和 InputBox ( )` 函数
10.1 MsgBox ( ) 函数
| Msgbox的作用是显示一个弹出式消息框,并提示用户点击一个命令按钮以继续进行下一个任务。Msgbox的结构如下:
yourMsg=MsgBox(Prompt, Style Value, Title) Msgbox 里的第一个参数 Prompt 是用来显示在消息框中的信讯息。 参数 Style Value 是确定什么类型的命令按钮出现在消息框中(请参阅表10.1). 参数Title 显示留言板上的标题
|
表10.1: Style Value
|
Style Value |
命名常数
|
显示的按钮 |
0 |
vbOkOnly | Ok button |
1 |
vbOkCancel | Ok and Cancel buttons |
2 |
vbAbortRetryIgnore | Abort, Retry and Ignore buttons. |
3 |
vbYesNoCancel | Yes, No and Cancel buttons |
4 |
vbYesNo | Yes and No buttons |
5 |
vbRetryCancel | Retry and Cancel buttons |
我们也可用命名常数来取代Style value 参数. 其实, VB6 会自动显示所有的命名常数,您只需要选择其中之一。
例子:
yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu")
和 yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu")
是一样的。
yourMsg 是个可持有由MsgBox ( ) 函数接受的价值的变数 . 这些价值是由各类型的按钮被点击而决定的。
表 10.2 : 返回值和命令按钮
值 |
常数 | 被点击的按钮 |
1 |
vbOk | Ok button |
2 |
vbCancel | Cancel button |
3 |
vbAbort | Abort button |
4 |
vbRetry | Retry button |
5 |
vbIgnore | Ignore button |
6 |
vbYes | Yes button |
7 |
vbNo | No button |
范例10.1
在图10.1里,你制定三个命令按钮和一个标签显示
![]()
|
ii. test button的程式:
Private Sub Test_Click()
Dim testmsg As Integer
testmsg = MsgBox("Click to test", 1, "Test message")
If testmsg = 1 Then
Display.Caption = "测试成功"
Else
Display.Caption = "测试成功"
End IfEnd Sub
当用户按一下测试按钮,图像10.2将会出现。当用户按一下确定按钮,该邮件“测试成功”会被显示出来,当他点击取消按钮,该邮件“测试失败”会被显示。
图 10.2

为了使消息框看起来更精密的,您可以添加一个图标 。VB中有四种类型的图标,如表10.3所显示
表 10.3
值 |
常数 |
图标 |
16 |
vbCritical |
|
32 |
vbQuestion |
|
48 |
vbExclamation |
|
64 |
vbInformation |
|
范例 10.2
图 10.3

Private Sub test2_Click()
Dim testMsg2 As Integer
testMsg2 = MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "Test Message")
If testMsg2 = 6 Then
display2.Caption = "测试成功"
ElseIf testMsg2 = 7 Then
display2.Caption = "您肯定吗?"
Else
display2.Caption = "测试失败"
End IfEnd Sub
10.2 InputBox( ) 函数
InputBox ( )函数将显示一个消息框以便用户可以输入一个值或一个信息。它的形式如下;
myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)
.
| Prompt |
通常显示为一个质询。
|
| Title | 输入框的标题。 |
| default-text | 在输入栏位显示的文字,,用户可以使用它来作为他的输入或他可能会改变的讯息。 |
| x-position and y-position |
输入框的位置或坐标
|
范例10.3
|
图10.4
![]() |
ii. OK 按钮的程式
Private Sub OK_Click()
Dim userMsg As String
userMsg = InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700)
If userMsg <> "" Then
message.Caption = userMsg
Else
message.Caption = "没有消息"
End IfEnd Sub
图10.5

- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-


