Experts in the group might find this question to be really easy and, therefore,
assume a trick question. It isn't one. This question comes up enough
that I thought I would throw it in for clarifty. Forms can be displayed in
one of two modes: modeless and modal. Modeless forms are normal windows display
on the screen. The main window of most forms is a modeless form. The
user can open multiple modeless forms and interact with each of them. Each
one has its own message loop. The lifetime of a modeless form is controlled
by the user (by clicking the close button) or programmatically. You display
a form modeless by using the Show method. When the method
returns the form is displayed and available for the user. The main (calling)
form can continue to do more work. Here is some sample code that will open
five modeless windows whenever a user clicks a button.
private void OnClicked ( object sender, EventArgs e )
{
for (int nIdx = 0; nIdx < 5; ++nIdx)
new Form().Show(this);
}
A modal form is similar to a modeless form but, once displayed, prevents the user
from interacting with the main (calling) form until the child form is dismissed.
The most common example is a dialog box (such as the open dialog). The modal
form has a special message loop that effectively prevents messages from reaching
the main form. When the user cancels the modal form control returns to the
parent. You display a form modal using ShowDialog.
The code will block at the method call until the modal form is closed.
Here is the same code that was given earlier. In this case though you have
to dismiss the form each time before the next one will appear.
private void OnClicked ( object sender, EventArgs e )
{
for (int nIdx = 0; nIdx < 5; ++nIdx)
new Form().ShowDialog(this);
}
Whether a form is modeless or modal impacts how you interact with it. A modeless
form can be closed at any time. Therefore if you are interested in getting
information out of the form you will have to hook into the form's events (such as
the close event). You can set properties on the form just like you would for
any other class but to get values back you'll have to use events. A modal
form, on the other hand, exists only for the lifetime of the block that creates
it. Therefore you can initialize the form's properties, display the form and
then get information back from the form without using events.
In general use modeless forms to display useful information or allow the user continual
access to information. The Visual Studio Solution Explorer, Output window
and Document windows would be examples of modeless forms. Use modal forms
when you need information from the user before continuing execution. Opening
a file or saving a document would be an example.