Visual Basic 2010 Lesson 12- Introduction to Functions

[Lesson 11] << [CONTENTS] >> [Lesson 13]

A VB function is a type of procedure that returns a value which is passed on to the main procedure to finish the execution. Therefore, it is different from a sub procedure which just perform some operations without returning a value. There are two types of functions in Visual Basic 2010, the built-in functions and the functions created by the programmers.The syntax of a function is

FunctionName (arguments)

The arguments are values that are passed on to the function.In this lesson, we will learn two very basic built-in functions of Visual Basic 2010 , i.e. the MsgBox( ) and the InputBox ( ) functions.




12.1 MsgBox ( ) Function

The objective of MsgBox is to produce a pop-up message box and prompts the user to click on a command button before he or she can continues. This syntax 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 to Table 12.1 for types of command button displayed. The Title argument will display the title of the message board.

Visual Basic 2010



We can use named constants in place of integers for the second argument to make the programs more readable. In fact, Visual Basic 2010 will automatically shows up a list of named constants where you can select one of them.

example: yourMsg=MsgBox( “Click OK to Proceed”, 1, “Startup Menu”)

and yourMsg=Msg(“Click OK to Proceed”. vbOkCancel,”Startup Menu”)

are the same.

yourMsg is a variable that holds values that are returned by the MsgBox ( ) function. The values are determined by the type of buttons being clicked by the users. It has to be declared as Integer data type in the procedure or in the general declaration section. Table 12.2 shows the values, the corresponding named constant and buttons.

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 Visual Basic 2010 as shown in Table 12.3

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 message

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.

The syntax to call up an Input Box is

Microsoft.VisualBasic.InputBox(Prompt, Title, default_text, x-position, y-position)

Example 12.3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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

The input box will appear as shown in the figure below when you press the command button

[Lesson 11] << [CONTENTS] >> [Lesson 13]

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on Tumblr