Home

Introduction to Dialog Boxes

     

Introduction

A dialog box is a form defined with particular properties. Like a form, a dialog box is referred to as a container. Like a form, a dialog box is mostly used to host child controls, insuring the role of dialog between the user and the machine. Here is an example of a dialog box:

The Run dialog box

A dialog box has the following characteristics:

  • The only system button it is equipped with is Close System Close. As the only system button, this button allows the user to dismiss the dialog and ignore whatever the user would have done on the dialog box
  • It cannot be minimized, maximized, or restored. A dialog box does not have any other system button but Close
  • It is usually modal, in which case the user is not allowed to continue any other operation on the same application until the dialog box is dismissed
  • It provides a way for the user to close or dismiss it

ApplicationApplication: Introducing Dialog Boxes

  1. Start Microsoft Visual Studio
  2. Create a new Windows Application named SolasPropertyRental1
  3. From the Common Controls section of the Toolbox, click Button and click the form
  4. While the button is selected on the form, in the Properties window, click (Name) and type btnNewProperty
  5. Click Text and type New Property
  6. To add another form to the project, on the main menu, click Project -> Add Windows Form...
  7. Set the Name to PropertyEditor and click Add
  8. From the Common Controls section of the Toolbox, click Button and click the form
  9. While the button is selected on the form, in the Properties window, click (Name) and type btnOK
  10. Click Text and type OK
  11. From the Common Controls section of the Toolbox, click Button and click the form
  12. While the button is selected on the form, in the Properties window, click (Name) and type btnCancel
  13. Click Text and type Cancel

Dialog Box Creation

To create a dialog box, you start with a form, which you can get by creating a Windows Application or deriving a class from Form. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    public Exercise()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        Text = "Domain Configuration";
        Width = 320;
        Height = 150;
        Location = new Point(140, 100);
        StartPosition = FormStartPosition.CenterScreen;
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

This would produce:

Starting a dialog box

Characteristics of Dialog Boxes

 

The Border Style of a Dialog Box

There are a few actions you should perform on a form to transform it into a dialog box; but normally, these are only suggestions, not rules. Based on the Microsoft Windows design and standards, to create a dialog box, you should set a form’s FormBorderStyle property to FixedDialog. Setting this property changes the borders of the form to the standard borders of a dialog box (the border of a dialog box is thinner than that of a regular form). You can set this characteristic in the Properties window or programmatically. Here is an example:

private void InitializeComponent()
{
        Text = "Domain Configuration";
        Width = 320;
        Height = 150;
        Location = new Point(140, 100);
        StartPosition = FormStartPosition.CenterScreen;

        FormBorderStyle = FormBorderStyle.FixedDialog;
}

The Minimize and Maximize Boxes

Besides taking care of the border, you should also set both the MinimizeBox and the MaximizeBox properties to False. This causes the window to display only the system Close button. You can set these characteristics in the Properties window or programmatically. Here is an example:

private void InitializeComponent()
{
        Text = "Domain Configuration";
        Width = 320;
        Height = 150;
        Location = new Point(140, 100);
        StartPosition = FormStartPosition.CenterScreen;

        FormBorderStyle = FormBorderStyle.FixedDialog;
        MinimizeBox = false;
        MaximizeBox = false;
}

This would produce:

Form

ApplicationApplication: Configuring a Dialog Box

  • To transform the PropertyEditor form into a dialog box, in the Properties window, change its characteristics as follows:
    FormBorderStyle: FixedDialog
    StartPosition:        CenterParent
    MinimizeBox:         False
    MaximizeBox:        False
    ShowInTaskbar:    False

Closing a Dialog Box

You should provide a way for the user to close the dialog box. A dialog box should have at least one button labeled OK. This button allows the user to acknowledge the message of the dialog box and close it by clicking the button. If the user press Enter, the dialog box should also be closed as if the OK button was clicked.

Accepting an Action

Often the user will be presented with various options on a dialog box and may be asked to make a decision on the available controls. Most of the time, if you are creating such a dialog box, besides the OK button, it should also have a Cancel button. The OK button should be the default so that if the user presses Enter, the dialog box would be closed as if the user had clicked OK. Clicking OK or pressing Enter would indicate that, if the user had made changes on the controls of the dialog box, those changes would be acknowledged and kept when the dialog box is closed and usually the changed values of the control would be transferred to another dialog box or form. Keep in mind that you are responsible for implementing this functionality.

To fulfill this functionality of the OK button, after adding it to a dialog box (or form), open the AcceptButton combo box in the Properties window for the form and select the name of the button.

ApplicationApplication: Accepting an Action

  1. Click an unoccupied area of the form
  2. In the Properties window, click the arrow of the AcceptButton field and select btnOK

Cancelling an Action

The Cancel button is used to allow the user to dismiss whatever changes would have been made on the controls of the dialog box. The dialog box should also be configured so that if the user presses Esc, the dialog box would be closed as if the user had clicked Cancel.

To fulfill this functionality of the Cancel button, after adding it to a dialog box (or form), open the CancelButton combo box in the Properties window for the form and select the name of the button.

Besides the OK and the Cancel buttons, a dialog box can be created with additional buttons such as Finish or Help, etc. It depends on its role and the decision is made by the application developer.

ApplicationApplication: Cancelling an Action

  1. In the Properties window, click the arrow of the CancelButton field and select btnCancel
     
    Solas Property Rental
  2. To display the first form, on the main menu, click Windows -> Form1.cs [Design]

The Help Button

Besides the system Close button, if you are planning to provide help on a dialog box, you can equip it with a Help button. To support this, the Form class is equipped with a Boolean property named HelpButton. The default value of this property is false. In the Properties window, you can set it to True. If you are programmatically creating the dialog box, you can access this property and set its value to true. Here is an example:

private void InitializeComponent()
{
        Text = "Domain Configuration";
        Width = 320;
        Height = 150;
        Location = new Point(140, 100);
        StartPosition = FormStartPosition.CenterScreen;

        FormBorderStyle = FormBorderStyle.FixedDialog;
        MinimizeBox = false;
        MaximizeBox = false;

        HelpButton = true;
}

This would produce:

Form

When the user clicks the help button, the mouse cursor becomes equipped with a question mark. Here is an example:

Form

You can then write code so that, when the user clicks a control on the dialog box, some guiding help is provided as a tool tip.

Modal and Modeless Dialog Boxes

 

Modal Dialog Boxes

There are two types of dialog boxes: modal and modeless.

A Modal dialog box is one that the user must first close in order to have access to any other framed window or dialog box of the same application. One of the scenarios in which you use a dialog box is to create an application that is centered around one. In this case, if either there is no other form or dialog box in your application or all the other forms or dialog boxes depend on this central dialog box, it must be created as modal. Such an application is referred to as dialog-based.

Some applications require various dialog boxes to complete their functionality. When in case, you may need to call one dialog box from another and display it as modal. Here is an example:

The Date and Time dialog box of WordPad is modal: when it is displaying, the user cannot use any other part of WordPad unless he or she closes this object first

Modal

After creating a dialog used as an addition to an existing form or an existing dialog box, to call it as modal, use the ShowDialog() method.

Modeless Dialog Boxes

A dialog box is referred to as modeless if the user does not have to close it in order to continue using the application that owns the dialog box. A modeless dialog box has the following characteristics

  • It has a thin border
  • It can be neither minimized nor maximized. This means that it is not equipped with the Minimize or the Maximize buttons
  • It is not represented on the taskbar with a button
  • It must provide a way for the user to close it

Here is an example:

The Find (and the Replace) dialog box of WordPad (also the Find and the Replace dialog boxes of most applications) is an example of a modeless dialog box. If it is opened, the user does not have to close it in order to use the application or the document in the background.

Modeless

Since the modeless dialog box does not display its button on the task bar, the user should know that the dialog box is opened. To make the presence of a modeless dialog box obvious to the user, it typically displays on top of its host application until the user closes it.

A modeless dialog box is created from a form but it should look like a regular dialog box or a tool window. Therefore, to create a modeless dialog box, set the FormBorderStyle property to an appropriate value such as FixedSingle, FixedToolWindow, Sizable or SizableToolWindow. Also, set its ShowInTaskbar property to False.

After creating the dialog box, to display it as modeless, call the Show() method. The fundamental difference between the ShowDialog() and the Show() methods is that the former displays a modal dialog box, which makes sure that the called dialog box cannot go in the background of the main application. By contrast, the Show() method only calls the dialog box every time it is requested. For this reason, it is up to you to make sure that the modeless dialog box always remains on top of the application. This is easily taken care of by setting the Boolean TopMost property of the form to True.

There are two main ways a normal modeless dialog box can be dismissed:

  • If the user has finished using it, he or she can close it and recall it at will
  • When the form or application that owns the modeless dialog box is closed, the form or application closes the modeless dialog if it is opened; this means that you don't need to find out whether a modeless dialog box is still opened when the application is being destroyed: either the user or the application itself will take care of closing it

An Application With Various Forms or Dialog boxes

When you create a Windows Forms Application, the starting form is made available to you. If one form is not enough for your application, you can add as many as necessary. To add (or to create) a (new) form, you have various options:

  • On the main menu, you can click Project -> Add New Item...
  • On the main menu, you can click File -> Add New Item...
  • In Solution Explorer, you can right-click the name of the project, position the mouse on Add, and click Add New Item...

In the Add New Item dialog box and in the Templates section, click Window Form (.NET), provide a name in the Name edit box then click Open.

If your application is using various forms and you want to display a particular one at design time:

  • In the Forms Designer, you can click the tab that corresponds to the desired form and that has [Design]
  • On the main menu, you can click Window and click the name of the form in the list under Close All Documents
  • In Solution Explorer, expand the Header Files node if necessary and double-click the name of the desired form that has the .h extension

If you visually add two (or more) forms to your application, you may need to link them, allow one to call the other. To do this, in the top section of the file, type #include followed by the name of the header file in which the form was defined. In the section where you want to access the form, declare a handle to the class of the form and use the new operator to allocate memory for it. To display the other form, you can call its Show() method.

ApplicationApplication: Using Various Forms

  1. Double-click the New Property... button and implement the event as follows:
    private void btnNewProperty_Click(object sender, EventArgs e)
    {
        PropertyEditor dlgEditor = new PropertyEditor();
    
        dlgEditor.ShowDialog();
    }
  2. To execute the application, on the main menu, click Debug -> Start Debugging
  3. On the form, click the New Property button to display the dialog box
  4. On the dialog box, click Cancel
  5. Close the form and return to your programming environment

An Automatic Dialog Box

Microsoft Visual Studio provides a fast way to create a modal dialog box. To get it, on the main menu, click Project -> Add New Item... In the middle list, click Dialog and provide a name:

Automatic Dialog Box

This would generate a dialog box with the necessary characteristics and two buttons: OK and Cancel:

Dialog Box

All you have to do is to design and customize the dialog box as you see fit.

 

Home Copyright © 2010-2011 C#Key