Lesson 12: Functions-Part I
A function is similar to a normal procedure but the main purpose of the function is to accept a certain input and return a value which is passed on to the main program to finish the execution. There are two types of functions, the built-in functions (or internal functions) and the functions created by the programmers.
The general format of a function is
FunctionName (arguments)
FunctionName (arguments)
The arguments are values that are passed on to the function.
In this lesson, we are going to learn two very basic but useful internal functions of Visual basic , i.e. the MsgBox( ) and InputBox ( ) functions.
12.1 MsgBox ( ) Function
The objective of MsgBox is to produce a pop-up message box and prompt the user to click on a command button before he /she can continues. This format is as follows:yourMsg=MsgBox(Prompt, Style Value, Title)
The first argument, Prompt, will display the message in the message box. The Style Value will determine what type of command buttons appear on the message box, please refer ton Table 12.1 for types of command button displayed. The Title argument will display the title of the message board.
Style Value | Named Constant | Buttons Displayed |
vbOkOnly | Ok button | |
vbOkCancel | Ok and Cancel buttons | |
vbAbortRetryIgnore | Abort, Retry and Ignore buttons. | |
vbYesNoCancel | Yes, No and Cancel buttons | |
vbYesNo | Yes and No buttons | |
vbRetryCancel | Retry and Cancel buttons |
example: yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu")
and yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu")
are the same.
Named Constant | Button Clicked | |
vbOk | Ok button | |
vbCancel | Cancel button | |
vbAbort | Abort button | |
vbRetry | Retry button | |
vbIgnore | Ignore button | |
vbYes | Yes button | |
vbNo | No button |
Example 12.1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testmsg As Integer
testmsg = MsgBox("Click to test", 1, "Test message")
If testmsg = 1 Then
MessageBox.Show("You have clicked the OK button")
Else
MessageBox.Show("You have clicked the Cancel button")
End If
End Sub
To make the message box looks more sophisticated, you can add an icon besides the message. There are four types of icons available in VB2008 as shown in Table 12.3
Named Constant | Icon | |
vbCritical | ||
vbQuestion | ||
vbExclamation | ||
vbInformation |
Example 12.2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim testMsg As Integer
testMsg = MsgBox("Click to Test", vbYesNoCancel + vbExclamation, "Test Message")
If testMsg = 6 Then
MessageBox.Show("You have clicked the yes button")
ElseIf testMsg = 7 Then
MessageBox.Show("You have clicked the NO button")
Else
MessageBox.Show("You have clicked the Cancel button")
End If
End Sub The first argument, Prompt, will display the messag
12.2 The InputBox( ) Function
An InputBox( ) function will display a message box where the user can enter a value or a message in the form of text. In VB2005, you can use the following format:
myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)myMessage is a variant data type but typically it is declared as string, which accept the message input by the users. The arguments are explained as follows:
- Prompt - The message displayed normally as a question asked.
- Title - The title of the Input Box.
- default-text - The default text that appears in the input field where users can use it as his intended input or he may change to the message he wish to enter.
- x-position and y-position - the position or tthe coordinates of the input box.
Microsoft.VisualBasic.InputBox(Prompt, Title, default_text, x-position, y-position)
The parameters remain the same.
Example 12.3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickThe inputbox will appear as shown in the figure below when you press the command button
Dim userMsg As String
userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700)
If userMsg <> "" Then
MessageBox.Show(userMsg)
Else
MessageBox.Show("No Message")
End If
End Sub
sage box. The Style Value will determine what type of command buttons appear on the message box, please refer Table 10.1 for types of command button displayed. The Title argument will display the title of the message board.
http://www.vbtutor.net/vb2008/vb2008tutor.html
No comments:
Post a Comment