Display reusable dialog forms in C#

[dialog]

The example Make a dialog with standard dialog features in C# shows the most common approach to making dialogs. Whenever it needs to display the dialog, the main program initializes a new instance of it and displays it. If the user clicks OK, the main program saves the dialog’s values.

This example shows another approach. Here the program creates an instance of the dialog when the main program starts and then it reuses the same instance whenever it needs to display the dialog.

If the main program is going to be able to reuse the dialog, the dialog cannot be destroyed when the users closes it. Instead of destroying the dialog, the program hides it so it can be reused later.

To let the user hide the dialog, it displays a Hide button that executes the following code.

// Hide the dialog.
private void btnHide_Click(object sender, EventArgs e)
{
    this.Hide();
}

The user could still try to close the dialog in other ways such as pressing Alt+F4 or clicking the X button in the form’s title bar. One way to prevent that is to set the dialog’s ControlBox property to false to remove the X, minimize, and maximize buttons. This works well if you don’t need to allow the user to minimize or maximize the dialog, but it’s inconvenient if the user may want to make the dialog really big.

This example takes another approach. It uses the following code to catch the dialog’s FormClosing event.

// Don't close, hide.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    this.Hide();
}

This code cancels the close and then hides the dialog instead of closing it.

The following code shows how the main program displays the dialog.

// The reusable form.
private ReusableForm TheReusableForm = new ReusableForm();

// Display the reusable form.
private void btnReusableForm_Click(object sender, EventArgs e)
{
    TheReusableForm.Show();
    TheReusableForm.Activate();
}

The class-level variable TheReusableForm holds a reference to the dialog and is initialized when the form loads.

When you click the Reusable Form button, the event handler displays the dialog and then calls its Activate method. That method sets focus to the dialog and pops it to the top. That is useful if the form is already visible and possibly behind the program’s main form or another program on the desktop.

You could pass the main form into the Show method to make the main form the dialog’s owner but that causes some problems. The trickiest is that the main form won’t close until its owned forms close. Because the dialog’s FormClosing event handler doesn’t allow the dialog to close, this is a problem. Working around that issue is a big enough hassle that it’s probably not worth it.

So why would you use this technique instead of the previous one that uses a new instance of the dialog every time it is needed? This method has the advantage that the dialog is displayed non-modally so the user could keep it up in the background at all times. (Note that you could also display it modally if you like.) That might make it useful for displaying and setting parameters that are used by the main program.

This technique also doesn’t require the main program to copy settings into and out of the dialog whenever it is displayed. That may be useful if the settings on the dialog are very complex and take a while to set up.

However, this technique is somewhat awkward and requires extra coordination between the main program and the dialog.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in dialogs, user interface and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *