Hi there
Am a new student in C# and am using Microsoft Visual C# 2013.
My aim is to
i. open a child form inside its MDIparent form
ii. disable the MDIparent form when the child form is active/opened

it was easier in VB.net with

frmStuDetails.ShowDialog()

I have tried

1.

MyChildForm childForm = new MyChildForm();
childForm.ShowDialog(this);

Result....but the problem is that the child form doesn't open within the MDIparent form/container

2.
under MDIparent call button

frmViewStuList childForm = new frmViewStuList(this);
childForm.Owner = this;
childForm.Show();

under child from Activated

 if (this.Owner != null)
  {
    this.Owner.Enabled = false;
  }

under child form Deactivate

 if (this.Owner != null)
 {
   this.Owner.Enabled = true;
 }

Result.....it makes the child form active but freezes the MDIparent when the child form closes

3.

ChildForm child = new ChildForm();
child.Owner = this;
child.Show();

// In ChildForm_Load:
private void ChildForm_Load(object sender, EventArgs e) 
{
  this.Owner.Enabled = false;
}

private void ChildForm_Closed(object sender, EventArgs e) 
{
  this.Owner.Enabled = true;
} 

Result ....It seems to be the best option but the child form doesn't open within the MDIparent

Please help if you have any other idea
Thanks

Edited 10 Months Ago by jeffrey4u

One option would be to just disable the controls on the MDIParent.

First set the MDIParent property insted of the Owner property.

Second add a static method to enable/disable the controls:

    public static void EnableControls(Form thisForm, bool enable = true)
    {
        if (thisForm.IsMdiContainer)
        {
            foreach (Control c in thisForm.Controls)
            {
                //The MDIChild is in the MdiClient.  This way it stays active.               
                if(c.GetType() != typeof(MdiClient))
                {
                    c.Enabled = enable;
                }
            }
        }
    }

In the MDIParent when you show the child also call this new method:

    MdiChildren[0].Show();
    EnableControls(this, false);

In the MDIChild FormClosed event handler re-enable the MDIParent's controls:

//Class of the MDIContainer
Form1.EnableControls(MdiParent);

Edited 10 Months Ago by tinstaafl

Guess the static method will be addedto both the child and the MDI parent form.
Is working as expected.
Thanks alot for your answer

Because it's static it just needs to be public in one class. Access it through the class name.

If this works for you please remember to mark this solved. Thanks

This question has already been answered. Start a new discussion instead.