Udostępnij za pośrednictwem


How to: Display Modal and Modeless Windows Forms

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.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To display a form as a modal dialog box

  • Call the ShowDialog method.

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

    Dim frmAbout as New Form()
    ' Display frmAbout as a modal dialog
    frmAbout.ShowDialog()
    
    //Display frmAbout as a modal dialog
    Form frmAbout = new Form();
    frmAbout.ShowDialog();
    
    Form ^ frmAbout = gcnew Form();
    //Display frmAbout as a modal dialog
    frmAbout->ShowDialog();
    

    The 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 Visual C#) as the owner of the dialog box to establish your main form as the owner, as the following code shows.

    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
    
    private void mnuAbout_Click(object sender, System.EventArgs e)
    {
       Form f = new Form();
       f.ShowDialog(this);
    }
    
    private:
       void mnuAbout_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          Form ^ f = gcnew 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.

    Dim f As New Form()
    ' Display f as a modeless dialog.
    f.Show()
    
    //Display f as a modeless dialog
    Form f= new Form();
    f.Show();
    
    Form ^ f = gcnew 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

Tasks

How to: Retrieve Dialog Box Information Selectively Using Multiple Properties

Walkthrough: Retrieving Dialog Box Information Collectively Using Objects

Reference

Windows Forms Overview

Other Resources

Creating a New Windows Form

Dialog Boxes in Windows Forms