Modal Versus Modeless

Picture an application comprised of several forms, where one form calls another, which may call yet another. Many MDI applications work this way. Generally speaking, you may keep all the forms open, moving among them as much as you'd like. This is called modeless behavior.

In contrast, consider the behavior of most dialog boxes in most applications. A user action causes a dialog box to be displayed, generally for the purpose of presenting information to or gathering input from the user. In the first case, the program cannot proceed until it knows that the information was presented to and acknowledged by the user. In the latter case, the program cannot proceed until either the requisite input has been gathered from the user or the user cancels the dialog. In either case, the program cannot proceed until the user has dismissed the dialog box. This is known as modal behavior.

Not all dialog boxes are modal. Common examples of modeless dialog boxes are those found in many word processors and text editors for finding text. As you search for text, the dialog box remains open and you can move freely between the dialog box and the document it is searching.

Even if a modal form or dialog box is opened, the user can still switch focus to other applications. Modal dialog boxes prevent the user from moving around in the current applicationi.e., the application that opened the dialog box, using either mouse or keyboard inputbut do not prevent the user from working in other applications.

A class of dialog boxes known as system modal prohibits moving to another application. These dialog boxes are generally used when Windows has a serious problem that must be addressed before any other activity can occur. The .NET Framework does not inherently support the creation of system modal dialog boxes.

Both forms and dialog boxes can be either modal or modeless, depending on how you invoke them. To make them modeless, call Show( ); to make them modal, call ShowDialog( ).

The following lines of code open a modal dialog:

Form frm = new Form( ); frm.ShowDialog( );

By changing the method call to Show (rather than ShowDialog), you invoke the same form, but modeless:

Form frm = new Form( ); frm.Show ( );

Good UI design suggests that you should use modal dialogs only when absolutely necessary.

Категории