Você está na página 1de 16

Electro-Team

Interesting Education Visual Basic 2010 Functions A

electroteam__@hotmail.com

Functions
Accept input and return a value. 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)
The arguments are values that are passed on to the
function.

Built-in Functions (I)

MsgBox ( ) Function
Produce a pop-up message box and prompt the user to click on a command button. Format:
MsgBox(Prompt, Style Value, Title)
Prompt: the message itself. Style Value: determine what type of command buttons appear.

Styles Value
Style Value
0

Named Constant
vbOkOnly

Buttons Displayed
Ok button

1
2 3 4 5

vbOkCancel
vbAbortRetryIgnore vbYesNoCancel vbYesNo vbRetryCancel

Ok and Cancel buttons


Abort, Retry and Ignore buttons. Yes, No and Cancel buttons Yes and No buttons Retry and Cancel buttons

Example
MsgBox("Hello World", vbOKCancel, "HW-Program")

MsgBox("Hello World", 1, "HW-Program")

Return Values
Value
1 2 3 4 5 6 7

Named Constant
vbOk vbCancel vbAbort vbRetry vbIgnore vbYes vbNo

Button Clicked
Ok button Cancel button Abort button Retry button Ignore button Yes button No button

Ex: Return Value


Dim x As Integer x = MsgBox("abcdefg", 1, "Eng") If x = 1 Then

MessageBox.Show("OK button")
Else MessageBox.Show("Cancel button") End If

Message Box With Icon


Value 16 Constant vbCritical Icon

32
48 64

vbQuestion
vbExclamation vbInformation

Ex: Message Box With Icon


Dim x As Integer

x = MsgBox("abcdef", vbYesNoCancel + vbExclamation, "Eng")


If x = 6 Then MessageBox.Show("Yes button") ElseIf x = 7 Then MessageBox.Show("NO button") Else MessageBox.Show("Cancel button") End If

The InputBox( ) Function


display a message with TextBox where the user can enter

a value or a message in the form of text.


Format:
Microsoft.VisualBasic.InputBox(Prompt, Title, default_text, xposition, y-position) Microsoft.VisualBasic.InputBox(Prompt, Title)

Ex: InputBox
Dim txt As String txt = Microsoft.VisualBasic.InputBox("What is your name?", "Names") If txt <> "" Then

MessageBox.Show(txt)
Else MessageBox.Show("No Message") End If

The Mid Function


Retrieve a part of text form a given phrase. Format:
Mid(phrase, position, n)
Phrase is the string from which a part of text is to be retrieved. Position is the starting position of the phrase from which the retrieving process begins. n Is the number of characters to retrieve.

Example
Dim txt As String txt = Microsoft.VisualBasic.InputBox("Enter your phrase") MsgBox(Mid(txt, 2, 6))

The Chr and the Asc functions


The Chr function returns the string that corresponds

to an ASCII code while the Asc function converts an


character or symbol to the corresponding ASCII code.

Format:
Chr(ASCII code)
Chr(65)=A

Asc(Character)
Asc("B")=66

Você também pode gostar