Unit-05
Unit-05
Unit 5
Function
Introduction
MsgBox is used to create a pop-up message box and prompt the user to
click on a command button before continues. InputBox( ) function display
a message box where the user can enter a value or a message in the form
of text. In this unit, we are going to learn two useful internal functions of
Visual basic, i.e. the MsgBox( ) and InputBox ( ) functions.
Outcomes
MsgBox ( ) Function
The objective of MsgBox is to create 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:
Msg=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 (Table 1) for types of command button displayed. The
Title argument will display the title of the message board.
Table 1: Style Values
100
Visual Programming
We can use named constants in place of integers for the second argument
to make the programs more readable.
Example:
testmsg = MsgBox("Click OK to Proceed", 1, "Startup")
and testmsg = MsgBox("Click OK to Proceed",
vbOKCancel, "Startup") are the same.
testmsg 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. Table .2 shows the values, the corresponding named
constant and buttons.
Table 2: Return Values and Command Buttons
Example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim testmsg As Integer
testmsg = MsgBox("Click OK to Proceed", 1, "Startup")
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
101
MsgBox( ) and InputBox ( ) functions.
To make the message box looks more stylish, you can add an icon besides
the message. There are four types of icons available in VB2008 as shown
in Table 3
Text displayed in a dialog should be descriptive and as short as possible.
Tip
Table 3: Icons
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 the coordinates of the
input box.
102
Visual Programming
Example
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim testmsg As String
testmsg =
Microsoft.VisualBasic.InputBox("Write your message?",
"input box test", "write your messge here", 500, 700)
The inputbox will appear as shown in the figure below when you press
the command button
103
MsgBox( ) and InputBox ( ) functions.
Assessment
MCQ
1. User can enter a value or a message in the form of text in
Assessment a) An InputBox( ) function
b) MsgBox ( ) Function
c) Both
d) None
2. Which constant, when passed to method MessageBox.Show, indicates
that a question is being asked?
a) MessageBox.Question
b)MessageBoxIcon.QuestionMark
c) MessageBox.QuestionMark
d)MessageBoxIcon.Question
3. The first argument passed to method MessageBox. Show is ____
a) the text displayed in the dialog‘s title bar
b) a constant representing the Buttons displayed in the dialog
c) the text displayed inside the dialog
d) a constant representing the icon that appears in the dialog
4. You can specify the Button(s) and icon to be displayed in a message
dialog by using the MessageBoxButtons and_____ constants.
a) MessageIcon b)MessageBoxImages
c) MessageBoxPicture d)MessageBoxIcon
Short Questions
1. What are Message Boxes? Discuss about the 'Msgbox' command with
example.
2. Which of the followings are same?
i. testmsg = MsgBox("Click yes to Proceed", 4, "Startup")
ii. testmsg = MsgBox("Click yes to Proceed", vbOkOnly, "Startup")
iii. testmsg = MsgBox("Click yes to Proceed", vbYesNo, "Startup")
104
Visual Programming
String Manipulation
Upon completion of this unit you will be able to:
Outcomes
Example 8.1
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim text1, text2, text3 As String
text1 = "Bangladesh"
text2 = " Open University"
text3 = text1 + text2
TextBox3.Text = text3
End Sub
The line text3=text1+ text2 can be replaced by text3=text1 & text2 and
produced the same output. However, if one of the variables is declared as
numeric data type, you cannot use the + sign, you can only use the &
sign. An error occurs if you do not following rule, as shown in the
following example.
105
String Manipulation
End Sub
106
Visual Programming
Right Function
The Right function extracts the right portion of a phrase. The format is
Right ("Phrase",n)
Where n is the starting position from the right of the phase where the
portion of the phrase is going to be extracted.
For example,
Microsoft.VisualBasic .Right (―Visual Basic‖, 5) = Basic
Let us look at another example.
Private Sub Button4_Click(ByVal sender As
107
String Manipulation
Left Function
The Left function extract the left portion of a phrase. The format is
Microsoft.VisualBasic.Left("Phrase",n)
Where n is the starting position from the left of the phase where the
portion of the phrase is going to be extracted. For example,
Microsoft.VisualBasic.Left (―Visual Basic‖, 6) = Visual.
Mid Function
The Mid function is used to retrieve a part of text form a given phrase.
The syntax of the Mid Function is
Mid(phrase, position,n)
Where
Example
Private Sub Button5_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button5.Click
Dim mtext As String
mtext = TextBox1.Text
108
Visual Programming
Trim(“Phrase”)
Example
Private Sub Button6_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button6.Click
Dim mtext As String
mtext = TextBox1.Text
TextBox3.Text = Trim(mtext)
End Sub
109
String Manipulation
Ltrim(“Phrase”)
For example,
For example,
Private Sub Button7_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button7.Click
Dim mtext As String
mtext = TextBox1.Text
TextBox2.Text = LCase(mtext)
TextBox3.Text = UCase(mtext)
End Sub
110
Visual Programming
Assessment
MCQ
1. Which of the following function returns an integer value which is the
length of a phrase or a sentence?
Assessment
a) len function
b) trim function
c) rtrim function
d) mid function
Short Questions
1. Which notes of the followings are same?
i. Len function
ii. Ucse and Lcase function
iii. Trim function
111