JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Using the MsgBox Function

Later in this chapter, we'll start discussing multiform applications, but there's an easy way to add multiple forms already built into Visual Basic—you can use the MsgBox and InputBox functions. You can also use the MessageBox class built into the .NET Framework to display messages and accept input from the user. You'll find all of these at work in the MsgAndInputBoxes example on the CDROM—all you need to do is to click a button to use these various functions. I'll start with the MsgBox function in this topic:

Public Function MsgBox(Prompt As Object [, Buttons As MsgBoxStyle =
MsgBoxStyle.OKOnly [, Title As Object = Nothing]]) As MsgBoxResultArguments

Here are the arguments you pass to this function:

  • Prompt—A string expression displayed as the message in the dialog box. The maximum length is about 1,024 characters (depending on the width of the characters used).

  • Buttons—The sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If you omit Buttons, the default value is zero. See below.

  • Title—String expression displayed in the title bar of the dialog box. Note that if you omit Title, the application name is placed in the title bar.

Tip 

If you want the message box prompt to be more than one line of text, you can force separate lines of text using a carriage return character (Chr(13)), a linefeed character (Chr(10)), or a carriage return/linefeed together (Chr(13) & Chr(10)) between each line.

You can find the possible constants to use for the Buttons argument in Table 4.4.

Table 4.4: MsgBox constants.

Constant

Value

Description

OKOnly

0

Shows OK button only.

OKCancel

1

Shows OK and Cancel buttons.

AbortRetryIgnore

2

Shows Abort, Retry, and Ignore buttons.

YesNoCancel

3

Shows Yes, No, and Cancel buttons.

YesNo

4

Shows Yes and No buttons.

RetryCancel

5

Shows Retry and Cancel buttons.

Critical

16

Shows Critical Message icon.

Question

32

Shows Warning Query icon.

Exclamation

48

Shows Warning Message icon.

Information

64

Shows Information Message icon.

DefaultButton1

0

First button is default.

DefaultButton2

256

Second button is default.

DefaultButton3

512

Third button is default.

ApplicationModal

0

Application modal, which means the user must respond to the message box before continuing work in the current application.

SystemModal

4096

System modal, which means all applications are unavailable until the user dismisses the message box.

MsgBoxSetForeground

65536

Specifies the message box window as the foreground window.

MsgBoxRight

524288

Text will be right-aligned.

MsgBoxRtlReading

1048576

Specifies text should appear as right-to-left on RTL systems such as Hebrew and Arabic.

Note also that this function returns a value from the MsgBoxResult enumeration. Here are the possible MsgBoxResult values, indicating which button in the message box the user clicked:

  • OK

  • Cancel

  • Abort

  • Retry

  • Ignore

  • Yes

  • No

For example, here's how we use MsgBox in the MsgAndInput example. In this case, I'm adding OK and Cancel buttons to the message box, adding an information icon, and making the message box modal (which means you have to dismiss it before doing anything else). And I also check to see if the user clicked the OK button, in which case I display the message "You clicked OK" in a text box:

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim Result As Integer
    Result = MsgBox("This is a message box!", MsgBoxStyle.OKCancel +
        MsgBoxStyle.Information + MsgBoxStyle.SystemModal, "Message Box")
    If (Result = MsgBoxResult.OK) Then
       TextBox1.Text = "You clicked OK"
    End If
End Sub

You can see the results of this code in Figure 4.5.


Figure 4.5: A message box created with the MsgBox function.
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor