We have introduced the basic concept of functions in the previous lesson. In this lesson, we will examine the built-in functions in VB2010. As a matter of facts, we have introduced three built-in functions in Lesson 8, they are the Len function, the Left function, and the Right Function. In this lesson, you will learn additional built-in functions.
The Mid function is used to retrieve a part of the text from a given phrase. The syntax is
Mid(phrase, position,n)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myPhrase As String myPhrase = Microsoft.VisualBasic.InputBox("Enter your phrase") Label1.Text = Mid(myPhrase, 2, 6) End Sub
* When the user clicks the command button, an input box will pop up asking the user to enter a phrase. After a phrase is entered and OK button is pressed, the label will show the extracted text starting from position 2 of the phrase and the number of characters extracted is 6, as shown in the figures below:
The Right function extracts the right portion of a phrase. The syntax is
Microsoft.Visualbasic.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", 4) = asic
The following code extracts the right portion any phrase entered by the user.
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myword As String myword = TextBox1.Text Label1.Text = Microsoft.VisualBasic.Right (myword, 4) End Sub
The Left function extracts the left portion of a phrase. The syntax is
Microsoft.Visualbasic.Right ("Phrase", n)
TWhere 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", 4) = asic
TThe following code extracts the left portion any phrase entered by the user.
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myword As String myword = TextBox1.Text Label1.Text = Microsoft.VisualBasic.Left (myword, 4) End Sub
TThe Trim function trims the empty spaces on both sides of the phrase. The format is
Trim("Phrase")
For example,
Trim (" Visual Basic 2010 ") = Visual basic 2010
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myPhrase As String myPhrase = Microsoft.VisualBasic.InputBox("Enter your phrase") Label1.Text = Trim(myPhrase) End Sub
TThe Ltrim function trims the empty spaces of the left portion of the phrase. The syntax is
Ltrim("Phrase")
TFor example,
Ltrim (" Visual Basic 2010 ")= Visual basic 2010
TThe Rtrim function trims the empty spaces of the right portion of the phrase. The syntax is
Rtrim("Phrase")
TFor example,
Rtrim ("Visual Basic ") = Visual Basic
TThe InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The syntax is
Instr (n, original phase, embedded phrase)
TWhere n is the position where the Instr function will begin to look for the embedded phrase. For example
Instr(1, "Visual Basic 2010 ","Basic")=8
T*The function returns a numeric value.
TYou can write a program code as shown below:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = InStr(1, "Visual Basic 2010", "Basic") End Sub
TThe Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters.
TThe syntax is
Microsoft.VisualBasic.UCase(Phrase) Microsoft.VisualBasic.LCase(Phrase)
TFor example,
Microsoft.VisualBasic.Ucase("Visual Basic 2010") =VISUAL BASIC 2010 Microsoft.VisualBasic.Lcase("Visual Basic 2010") =visual basic 2010
The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for "American Standard Code for Information Interchange". Altogether there are 255 ASCII codes and as many ASCII characters. Some of the characters may not be displayed as they may represent some actions such as the pressing of a key or produce a beep sound. The syntax of the Chr function is
Chr(charcode)
Tand the syntax of the Asc function is
Asc(Character)
TThe following are some examples:
Chr(65)=A, Chr(122)=z, Chr(37)=% Asc("B")=66, Asc("&")=38
TThe 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)
TThe 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.
Style Value | Named Constant | Buttons Displayed |
---|---|---|
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 |
TWe 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 show up a list of named constants where you can select one of them.
Examples:
yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu")and
yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu")
TyourMsg 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 13.2 shows the values, the corresponding named constant and buttons.
Value | Named Constant | Button Clicked |
---|---|---|
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 |
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
TTo make the message box looks more sophisticated, you can add an icon beside the message. There are four types of icons available in Visual Basic 2010 as shown in Table 13.3
Value | Named Constant | Icon |
---|---|---|
16 | vbCritical | |
3 | vbQuestion | |
48 | vbExclamation | |
64 | vbInformation |
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
TThe first argument, Prompt, will display the message
TAn 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)
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
Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Privacy Policy