MsgBox Function

   
MsgBox Function

Class

Microsoft.VisualBasic.Interaction

Syntax

 MsgBox(   prompt   [,   buttons   ][,   title   ]) 
prompt (required; String)

The text of the message to display in the message box dialog box

buttons (optional; MsgBoxStyle enumeration)

The sum of the Button , Icon , Default Button , and Modality constant values

title (optional; String)

The title displayed in the title bar of the message box dialog box

Return Value

A MsgBoxResult enumeration constant indicating the button clicked by the user to close the message box

Description

Displays a dialog box containing a message, buttons, and optional icon to the user. The action taken by the user is returned by the function in the form of an enumerated constant.

Rules at a Glance

  • prompt can contain approximately 1,000 characters , including carriage return characters such as the built-in vbCrLf constant.

  • If the title parameter is omitted, the name of the current application or project is displayed in the title bar.

  • If you omit the buttons argument, the default value is 0; that is, VB opens an application modal dialog box containing only an OK button.

  • The constants of the MsgBoxStyle enumeration can be added together to form a complete buttons argument. The constants can be divided into the following groups:

    Button Display Constants
    Icon Display Constants
    Default Button Constants
    Modality Constants

    Only one constant from each group can be used to make up the overall buttons value.

Button Display Constants

Constant

Value

Buttons to display

 MsgBoxStyle.OKOnly 

OK only

 MsgBoxStyle.OKCancel 

1

OK and Cancel

 MsgBoxStyle.AbortRetryIgnore 

2

Abort, Retry, and Ignore

 MsgBoxStyle.YesNoCancel 

3

Yes, No, and Cancel

 MsgBoxStyle.YesNo 

4

Yes and No

 MsgBoxStyle.RetryCancel 

5

Retry and Cancel

Icon Display Constants

Constant

Value

Icon to display

 MsgBoxStyle.Critical 

16

Critical Message

 MsgBoxStyle.Question 

32

Warning Query

 MsgBoxStyle.Exclamation 

48

Warning Message

 MsgBoxStyle.Information 

64

Information Message

Default Button Constants

Constant

Value

Default button

 MsgBoxStyle.DefaultButton1 

First button

 MsgBoxStyle.DefaultButton2 

256

Second button

 MsgBoxStyle.DefaultButton3 

512

Third button

 MsgBoxStyle.DefaultButton4 

768

Fourth button

Modality Constants

Constant

Value

Modality

 MsgBoxStyle.ApplicationModal 

Application

 MsgBoxStyle.SystemModal 

4096

System

Return Values

The following intrinsic constants can be used to determine the action taken by the user and represent the value returned by the MsgBox function:

Constant

Value

Button clicked

 MsgBoxResult.OK 

1

OK

 MsgBoxResult.Cancel 

2

Cancel (or Esc key pressed)

 MsgBoxResult.Abort 

3

Abort

 MsgBoxResult.Retry 

4

Retry

 MsgBoxResult.Ignore 

5

Ignore

 MsgBoxResult.Yes 

6

Yes

 MsgBoxResult.No 

7

No

If the MsgBox contains a Cancel button, the user can press the Esc key and the function's return value will be that of the Cancel button.

Programming Tips and Gotchas

  • Application modality means that the user cannot access other parts of the application until a response to the message box has been given. In other words, the appearance of the message box prevents the application from performing other tasks or from interacting with the user other than through the message box.

  • System modality used to mean that all applications were suspended until the message box was closed. However, with multitasking operating systems, such as Windows 95 and Windows NT, this is not the case. Basically, the message box is defined to be a "Topmost" window that is set to "Stay on Top," which means that the user can switch to another application and use it without responding to the message box. But because the message box is the topmost window, it will be positioned on top of all other running applications.

  • Unlike its InputBox counterpart , MsgBox cannot be positioned on the screen. It is always displayed in the center of the screen.

  • If your application is to run out-of-process on a remote machine, you should remove all MsgBox functions since they will not be displayed to the user, but instead will appear on the monitor of the remote server!

  • MsgBox should never be used in ASP.NET applications.

VB.NET/VB 6 Differences

In VB 6, the MsgBox function has five parameters. The last two, helpfile (which specified the path to a help file containing information about the error message) and context (which specified the help context ID within helpfile ), are optional. In VB.NET, these two parameters are not supported.

See Also

InputBox Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net