Visual Basic Concepts

Visual Studio 6.0

Modal and Modeless Dialog Boxes

Dialog boxes are either modal or modeless. A modal dialog box must be closed (hidden or unloaded) before you can continue working with the rest of the application. For example, a dialog box is modal if it requires you to click OK or Cancel before you can switch to another form or dialog box.

The About dialog box in Visual Basic is modal. Dialog boxes that display important messages should always be modal — that is, the user should always be required to close the dialog box or respond to its message before proceeding.

Modeless dialog boxes let you shift the focus between the dialog box and another form without having to close the dialog box. You can continue to work elsewhere in the current application while the dialog box is displayed. Modeless dialog boxes are rare. From the Edit menu, the Find dialog box in Visual Basic is an example of a modeless dialog box. Use modeless dialog boxes to display frequently used commands or information.

To display a form as a modal dialog box

  • Use the Show method with a style argument of vbModal (a constant for the value 1).

    For example:

    ' Display frmAbout as a modal dialog.
    frmAbout.Show vbModal
    

To display a form as a modeless dialog box

  • Use the Show method without a style argument.

    For example:

    ' Display frmAbout as a modeless dialog.
    frmAbout.Show
    

Note   If a form is displayed as modal, the code following the Show method is not executed until the dialog box is closed. However, when a form is shown as modeless, the code following the Show method is executed immediately after the form is displayed.

The Show method has another optional argument, owner, that can be used to specify a parent-child relationship for a form. You can pass the name of a form to this argument to make that form the owner of the new form.

To display a form as a child of another form

  • Use the Show method with both style and owner arguments.

    For example:

    ' Display frmAbout as a modeless child of frmMain.
    frmAbout.Show vbModeless, frmMain
    

Using the owner argument with the Show method ensures that the dialog box will be minimized when it’s parent is minimized, or unloaded should the parent form be closed.

Show: