Home Search Contact us About us
Title Creating and displaying a simple dialog box, modeless and modal.
Summary This simple tutorial will guide you the process of adding and displaying a simple dialog / form in your application. It covers both modal and modeless dialogs.
Contributor John McTainsh
Published 6-Jun-2001
Last updated 6-Jun-2001
Page rating   97% for 7 votes Useless Brilliant

Description.

Forms / Dialogs are a fundamental part of most applications. Here we will show how to build, display and interact with a dialog. It will be built using both Modal and Modeless types. This is a very simple task so don't get lost in the details.

Create a simple dialog.

The following steps will create a Form (Dialog) in your current project. The dialog will have an OK button and a edit box.

  • Using Visual Studio.NET open the the project you want to add the Dialog to.
  • From the Project menu select Add Windows Form...
  • In Categories select Local Project Items (default).
  • In the Name field type the name of the class to call the form. include a .cs extension. It is nice to prefix the name with Frm ie FrmOpenFile. If you have trouble thinking of names look into using Hungarian notation.
  • In Templates select Windows Form.
  • Select OK. The Form will appear.
  • From the View menu select Toolbox.
  • Drag a Text box onto the new form.
  • Right click on the text box and select properties.
  • Change (Name) to m_tbAddress.
  • Change Modifiers to Public.
  • From the Toolbox drag a Button onto the form.
  • Right click on the button and select properties.
  • Change Text to "OK".
  • Change DialogResult to OK.

Displaying a Modal Dialog box.

A Modal dialog takes control of the applications user interface requiring the dialog to be dealt with and closed before other views of the application are able to receive input from the user. For example the File Open dialog prohibits the user doing anything with that application until the dialog is cancelled or a file is selected for opening.

The following creates the dialog box, but does not display it. The m_tbAddress property is set. This will pre-fill the data. ShowDialog displays the dialog and waits for the dialog to close before continuing. If the return code from the Show is OK the data is read back from the dialog and displayed.

    FrmAddress dlg = new frmAddress();
    dlg.m_tbAddress.Text = "Enter address here";
    if( dlg.ShowDialog( this ) == DialogResult.OK )
    {
        MessageBox.Show( dlg.m_tbAddress.Text );
    }

Displaying a Modeless Dialog box (Popup).

A Modeless dialog allows both the owner and sub windows to receive user input. The owner window can be forced to always appear beneath the Modeless window by calling AddOwnedForm. However, if this is not called the modeless window can appear in front of behind the owner window depending on who has focus. An example of a owned modeless dialog would be a tool bar when it is not docked.

The modeless window is slightly more difficult to process because it must notify the parent when it has been destroyed. If you fail to do this you should check if the form is Visible or Disposed before doing any thing with it.

The following  code creates a modeless dialog that is toggled into existence. When first called it is created. The next time it is called it is destroyed.

    // Declared as a member variable for the Dialog.
    private FrmStatus m_frmStatus;   

    ...

    // In some button handler
    if( m_frmStatus == null || !m_frmStatus.Visible )
    {
        // Create a new instance of the modeless dialog
        m_frmStatus = new DlgStatus();
        m_frmStatus.ShowInTaskbar = false;
        AddOwnedForm( m_frmStatus );
        m_frmStatus.Show();
    }
    else
    {
        // Destroy the modless dialog
        m_frmStatus.Dispose();
        m_frmStatus = null;
    }

To get the modeless to disappear when the OK button is pressed we need to add the following code to the form code. Open the form in design mode and double click on the OK button. This will take you to the code view. Insert Dispose() as shown. Note: I have named the button "m_btnOK" in place of the default "button1"

    protected void m_btnOK_Click (object sender, System.EventArgs e)
    {
        Dispose();
    }

Usually we would also add some code to inform the owner that we are done with the form. Remember, always take the time to name your variables intuitively. A few extra characters here and there can make the difference between happiness and a bawled head 3 months later when you return to add a feature.

Comments Date
Good Job : ) 24-Jan-2003 Gian
thanks...
notifying parent of modeless dialog closing 21-Jan-2004 Brian
I`m interested in a brief example of how to tell the parent window that the modeless child has closed. Can you refer me to an example somewhere or post one yourself?

Thanks,
modless windows disappear 18-Feb-2005 Carol
I have two modless windows that a messagbox opens on top of them. The modal message box does not dispose of them. Checking the dispose of property in the debugger the dispose of property is false. I don`t understand why they disapper. Any insight into this problem?
Home Search Contact us About us