Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / Windows Forms

MDI child as dialog form (MDI modal workaround)

Rate me:
Please Sign up or sign in to vote.
4.05/5 (14 votes)
21 Dec 2009CPOL2 min read 85.9K   3.5K   25   17
Ever had the need to block all other forms and controls when a specific form is shown as a dialog? This is a very simple workaround, for an MDI modal form.

Image 1

Introduction

Ever had the need to block all other forms and controls when a specific form is shown as a dialog? This is a very simple workaround, for an MDI modal form.

Background

I was designing an MDI rich application, where I needed a dialog form showing only in the parent form, and blocking the rest of the application, until the user selected something on the dialog form.

Using the code

The code is very, very simple; just inherit the MDIParent form on your MDI parent form instead of the normal form inheritance.

C#
public partial class Your_MDI_Parent_Form : MdiParent

To show a form as a dialog and block other controls, call the ShowChildDialog method on the parent form, like this:

C#
ChildForm frm = new ChildForm();
ShowChildDialog(frm, ChildForm_DialogReturned);

And, to receive the DialogResult from the child form, use the following event receiver:

C#
private void ChildForm_DialogReturned(object sender, DialogResultArgs e)
{
    MessageBox.Show("ChildForm returned: " + e.Result.ToString());
    ((Form)sender).Dispose();
}

Explanation of the code

The ShowChildDialog method

C#
public void ShowChildDialog(Form frm, 
       EventHandler<DialogResultArgs> DialogReturnedValue)
{
    frm.MdiParent = this;
    frm.MaximizeBox = false;
    frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
    callingsender = frm;
    DialogReturning += DialogReturnedValue;
    DisableControls();
    frm.Show();
}
  1. Set the MDI parent so the form is shown within the parent.
  2. Remove the Maximize button on the child form.
  3. Add an event receiver for the Closed event on the child form; this will be used to return the dialog result from the child dialog form.
  4. callingsender is for internal use, so the code knows which form it is dealing with.
  5. Add an event receiver for the returned dialog result.
  6. Calling the DisableControls method will disable all other controls and forms in the MdiParent.
  7. Last, show the form. This will be the only one enabled.

The DisableControls method

C#
private void DisableControls()
{
    for (int i = 0; i < this.Controls.Count; i++)
    {
        if (this.Controls[i].GetType() != typeof(MdiClient))
            this.Controls[i].Enabled = false;
    }
    foreach (Form frm in MdiChildren)
        frm.Enabled = false;
    if(callingsender != null)
        callingsender.Enabled = true;
}
  1. Loop through all the controls of the MDI parent and disable them if it not an MDI child control.
  2. Loop through all the child forms and disable them.
  3. If callingsender is not null, then enable it again.

The ForceReleaseOfControls method

C#
public void ForceReleaseOfControls()
{
    for (int i = 0; i < this.Controls.Count; i++)
        this.Controls[i].Enabled = true;
    
    foreach (Form frm in MdiChildren)
        frm.Enabled = true;
}
  1. Loop through all the controls and enable them again.
  2. Loop through all the child forms and enable them again.

What the code does not do...

This code does not check for the pre-enabled-state of controls and forms. Therefore, it will not remember what enable state the control/form was before, and set it back to that. This can be achieved pretty simply by having a collection of keys and values. But for now, this is the code. Hope it helps those who have the same issues as I did.

Point of interest

I learnt something new about MDI applications.

History

  • 21. Dec. 2009: First post.
  • 21. Dec. 2009 (later): Updated the article with some explanation of the code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Denmark Denmark

Comments and Discussions

 
QuestionDispose dialog Pin
Member 134787855-Jan-19 1:50
Member 134787855-Jan-19 1:50 
QuestionVB20012 - Tengo un error Pin
vchavez32315-Jun-13 1:17
vchavez32315-Jun-13 1:17 
QuestionExcelent usefull code!!! Pin
marianomdq28-Jun-12 21:40
marianomdq28-Jun-12 21:40 
GeneralRe: Excelent usefull code!!! Pin
Paw Jershauge28-Jun-12 22:23
Paw Jershauge28-Jun-12 22:23 
GeneralRe: Excelent usefull code!!! Pin
marianomdq29-Jun-12 16:08
marianomdq29-Jun-12 16:08 
GeneralMy vote of 5 Pin
imransandh26-Feb-12 18:31
imransandh26-Feb-12 18:31 
GeneralMy vote of 3 Pin
h7lmi29-Oct-11 16:10
h7lmi29-Oct-11 16:10 
QuestionInteresting but .... does not work in VBasic Pin
arzamm29-Sep-11 13:36
arzamm29-Sep-11 13:36 
AnswerRe: Interesting but .... does not work in VBasic Pin
Paw Jershauge30-Sep-11 12:23
Paw Jershauge30-Sep-11 12:23 
GeneralRe: Interesting but .... does not work in VBasic Pin
arzamm3-Oct-11 6:54
arzamm3-Oct-11 6:54 
GeneralRe: Interesting but .... does not work in VBasic Pin
arzamm3-Oct-11 8:33
arzamm3-Oct-11 8:33 
GeneralRe: Interesting but .... does not work in VBasic Pin
Paw Jershauge4-Oct-11 1:43
Paw Jershauge4-Oct-11 1:43 
GeneralRe: Interesting .... and it works good in VBasic Pin
arzamm4-Oct-11 7:13
arzamm4-Oct-11 7:13 
GeneralMy vote of 3 Pin
jonthan3411-Aug-10 9:16
jonthan3411-Aug-10 9:16 
GeneralRe: My vote of 3 Pin
Paw Jershauge26-Oct-10 2:27
Paw Jershauge26-Oct-10 2:27 
GeneralMy vote of 2 Pin
behzad200011-Mar-10 6:28
behzad200011-Mar-10 6:28 
GeneralRe: My vote of 2 Pin
Paw Jershauge25-Mar-10 8:55
Paw Jershauge25-Mar-10 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.