-1

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?

dandan78
  • 13,328
  • 13
  • 64
  • 78
Paramu
  • 613
  • 2
  • 10
  • 23

1 Answers1

1

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
    }
}
mj82
  • 5,193
  • 7
  • 31
  • 39
  • Hi Thanks For Kind Reply..Actually ToolStrip1 is available in Form2, and after docking mdichild form3 this Toolstrip1 is still remain to Form3 also,because form3 FormBorderStyle=None thats good. But while I close form3 its closing – Paramu May 05 '11 at 10:57
  • Hi Thanks For Kind Reply..Actually ToolStrip1 is available in Form2, and after docking mdichild form3 FormBorderStyle=None and this Toolstrip1 is still remain to Form3 also, thats good. But still while I close form3 its closing MdiParent also. Thanks For Reply – Paramu May 05 '11 at 11:05
  • I don't get it :) If you have Form3.FormBorderStyle = None, then you don't have Form3 close button. So, how do you close it? Do you have some button on Form3 to close it? If yes - put `this.Close();` in it's click event and it works. – mj82 May 05 '11 at 11:15
  • Hi Thanks ..I don't have any button on form3. I Dock from MdiParent Form2 only. So when I open ChildForm form3 from ToolstripButton, I need to close also...For that I need ur advice... But the same code I did in Vb.Net2008 it works fine... – Paramu May 05 '11 at 11:25