-1

实际上它是我的第一个项目。虽然我想将我的 VB.Net2008 转换为 C#2010,但我几乎没有澄清。

Form2我设置的属性中 - IsMDIContainer = True。然后下面的代码打开我的MdiChild,现在当我点击关闭按钮时我的问题是什么,它也在关闭MDIParent. 但我只需要关闭 mdichild ... 为此我尝试通过放置在以下代码中的 Vb.Net2008 样式,但MDIParent Form2它不起作用。任何正确的方向...

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();
  }
}

对我有什么正确的方向吗?

4

1 回答 1

1

我不确定我是否理解您想要实现的目标:您是否希望在单击父表单关闭按钮而不是关闭父表单时关闭所有子表单?Form2 是您的主窗体(父 MDI 容器),Form3 是 MDI 子窗体,不是吗?
请尝试以下代码并判断它是否是您要求的:

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
    }
}
于 2011-05-05T09:40:39.310 回答