Saturday, 8 February 2014



Displays a message in a dialog box and wait for the user to click a button, and returns an integer indicating which button the user clicked.

Following is an expanded MessageBox








Syntax : MsgBox ( Prompt [,icons+buttons ] [,title ] )

 memory_variable = MsgBox ( prompt [, icons+ buttons] [,title] )

 Prompt : String expressions displayed as the message in the dialog box. If prompt consist of more than one line, you can separate the lines using the vbrCrLf constant.

 Icons + Buttons : Numeric expression that is the sum of values specifying the number and type of buttons and icon to display.

 Title : String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.

 Icons:

Constant
Value
Description
vbCritical
16
Display Critical message icon
vbQuestion
32
Display Warning Query icon
vbExclamation
48
Display Warning message icon
vbInformation
64
Display information icon

Buttons:

Constant
Value
Description
vbOkOnly
0
Display OK button only
vbOkCancel
1
Display OK and Cancel buttons
vbAbortRetryIgnore
2
Display Abort, Retry and Ignore buttons
vbYesNoCancel
3
Display Yes, No and Cancel buttons
vbYesNo
4
Display Yes and No buttons
vbRetryCancel
5
Display Retry and Cancel buttons

Return Values

Constant
Value
Description
vbOk
1
Ok Button
vbCancel
2
Cancel Button
vbAbort
3
Abort Button
vbRetry
4
Retry Button
vbIgnore
5
Ignore Button
vbYes
6
Yes Button
vbNo
7
No Button

Following is an example illustrates the use of message boxes


Open a new Project and save the Form as messageboxdemo.frm and save the Project as messageboxdemo.vbp

 Design the application as shown below.
Object
Property
Setting
Form

Caption
Name
MessageBoxDemo
frmMessageBoxDemo
Label

Caption
Name
lblName
Name
TextBox

Name
Text
txtName
( empty )
ListBoxNamelstName
CommandButton

Caption
Name
Add
cmdAdd
CommandButton

Caption
Name
Delete
cmdDelete
CommandButton

Caption
Name
Exit
cmdExit



 Following code is entered in the txtName_Change ( ) event

 Private Sub txtName_Change() If Len(txtName.Text) > 0 Then cmdAdd.Enabled = True End If End Sub

 Following code has to be entered in the cmdAdd_Click ( ) event

 Private Sub cmdAdd_Click() answer = MsgBox("Do you want to add this name to the list box?", vbExclamation + vbYesNo, "Add Confirm") If answer = vbYes Then lstName.AddItem txtName.Text txtName.Text = "" txtName.SetFocus cmdAdd.Enabled = False End If End Sub

 Following code is entered in the cmdDelete_Click ( ) event Private Sub cmdDelete_Click() Dim remove As Integer remove = lstName.ListIndex If remove < 0 Then MsgBox "No names is selected", vbInformation, "Error" Else answer = MsgBox("Are you sure you want to delete " & vbCrLf & "the selected name?",_ vbCritical + vbYesNo, "Warning") If answer = vbYes Then If remove >= 0 Then lstName.RemoveItem remove txtName.SetFocus MsgBox "Selected name was deleted", vbInformation, "Delete Confirm" End If End If End If End Sub Following code is entered in the cmdExit_Click ( ) event Private Sub cmdExit_Click() answer = MsgBox("Do you want to quit?", vbExclamation + vbYesNo, "Confirm") If answer = vbYes Then End Else MsgBox "Action canceled", vbInformation, "Confirm" End If End Sub

 Save and run the application. You can notice the different type of message box types are used to perform an action

( Download the source code )
Instagram


0 komentar:

Post a Comment