Export (0) Print
Expand All

Displaying Modal and Modeless Windows Forms

Visual Studio .NET 2003

Forms and dialog boxes are either modal or modeless. A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application. For more information about working with dialog boxes, see User Input to Dialog Boxes.

Dialog boxes that display important messages should always be modal. The About dialog box in Visual Studio is an example of a modal dialog box. MessageBox is a modal form you can use.

Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.

Modeless forms are harder to program, because users can access them in an unpredictable order. You have to keep the state of the application consistent no matter what the user does. Often, tool windows are shown in a modeless fashion. The Find dialog box, accessible from the Edit menu in Visual Studio, is an example of a modeless dialog box. Use modeless forms to display frequently used commands or information.

To display a form as a modal dialog box

  • Call the ShowDialog method.

    The following example shows how to display a dialog box modally.

    ' Visual Basic
    Dim frmAbout as New Form()
    ' Display frmAbout as a modal dialog
    frmAbout.ShowDialog()
    
    // C#
    //Display frmAbout as a modal dialog
    Form frmAbout = new Form();
    frmAbout.ShowDialog();
    
    // C++
    Form * frmAbout = new Form();
    //Display frmAbout as a modal dialog
    frmAbout->ShowDialog();
    

    The Form.ShowDialog method has an optional argument, owner, that can be used to specify a parent-child relationship for a form. For example, when code in your main form shows a dialog box, you can pass Me (in Visual Basic) or this (in C#) as the owner of the dialog box to establish your main form as the owner, as the following code shows.

    ' Visual Basic
    Private Sub mnuAbout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
       Dim f As New Form()
       f.ShowDialog(Me)
    End Sub
    
    // C#
    private void mnuAbout_Click(object sender, System.EventArgs e)
    {
       Form f = new Form();
       f.ShowDialog(this);
    }
    
    // C++
    private:
       System::Void mnuAbout_Click(System::Object *  sender,
          System::EventArgs *  e)
       {
          Form * f = new Form();
          f->ShowDialog(this);
       }
    

To display a form as a modeless dialog box

  • Call the Show method.

    The following example shows how to display an About dialog box in modeless format.

    ' Visual Basic
    Dim f As New Form()
    ' Display frmAbout as a modeless dialog.
    f.Show()
    
    // C#
    //Display frmAbout as a modeless dialog
    Form f= new Form();
    f.Show();
    
    // C++
    Form * f = new Form();
    //Display f as a modeless dialog
    f->Show();
    
    Note   If a form is displayed as modal, the code following the ShowDialog 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.

See Also

Introduction to Windows Forms | Creating Windows Forms | Walkthrough: Retrieving Dialog Box Information Selectively Using Multiple Properties | Retrieving Dialog Box Information Collectively Using Objects | Dialog Boxes in Windows Forms

Show:
© 2014 Microsoft