Question

Actually Its my first project. While I want to convert my VB.Net2008 to C#2010 I have few clarification pls.

In Form2 Properties I set - IsMDIContainer = True. Then the below code to open my MdiChild and now what is my problem when I click the close button, it's also closing the MDIParent. But I need to close only mdichild... for that I tried as like Vb.Net2008 style by the following codes placed in MDIParent Form2, Its not working. Any right directions ...

private void toolStripButton1_Click(object sender, EventArgs e)
{
  Form3 NwMdiChild2 = new Form3;
  NwMdiChild2.MdiParent = this;
  NwMdiChild2.Dock = System.Windows.Forms.DockStyle.Fill;
  NwMdiChild2.Show();
}

private void Form2_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
  Form[] MdiChildForms = this.MdiChildren;
  int kkk1 = MdiChildForms.Length;
  int x = 0;
  for (x = 0; x <= MdiChildForms.Length - 1; x += 1) 
  {
    if (MdiChildForms[x].Name == "Form1")
    {
      kkk1 = kkk1 - 1;
    }
    MdiChildForms[x].Close();
  }
  if (kkk1 > 0) 
  {
  // For Not Closing
  e.Cancel = true;
  }
  else
  {
    // For Closing
    e.Cancel = false;
    Application.Exit();
  }
}

Any Right Directions for Me?

Was it helpful?

Solution

I'm not sure if I understand well what you want to achieve: do you want, when click Parent Form close button, insteed of closing parent form, to close all the child's form? Form2 is your main form (parent MDI container), Form3 is the MDI children, isn't it?
Please try following code and tell if it's what you are asking for:

private void toolStripButton1_Click(object sender, EventArgs e)
{
    Form3 NwMdiChild2 = new Form3();    //don't forget ()
    NwMdiChild2.MdiParent = this;
    NwMdiChild2.Dock = System.Windows.Forms.DockStyle.Fill;
    NwMdiChild2.Show();
}

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    //if no MDI child - this is going to be skipped and norlmal Form2 close takes place
    if (this.MdiChildren.Length > 0)    //close childrens only when there are some
    {
        foreach (Form childForm in this.MdiChildren)
            childForm.Close();

        e.Cancel = true;  //cancel Form2 closing
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top