0

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 by jeffrey4u

2
Contributors
4
Replies
17
Views
1 Year
Discussion Span
Last Post by jeffrey4u
0

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 by tinstaafl

0

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

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.