简体   繁体   English

我无法再次打开 MDI Parent 中的 2 个 Window Childs

[英]I Can't open again the 2 Window Childs in MDI Parent

I created a MDI Parent Form (Form1), that has 1 Menu with 2 Items.我创建了一个 MDI 父窗体 (Form1),它有 1 个菜单和 2 个项目。 Each Item opens a Window Child (Form2 and Form3).每个 Item 打开一个 Window Child(Form2 和 Form3)。 I can open Form2 and Form3, if they are already open they just got the focus.我可以打开 Form2 和 Form3,如果它们已经打开,它们就会得到焦点。 So far, everything's Ok.到目前为止,一切正常。 The problem is when i close Form2 and Form3, i can't open them again?问题是当我关闭 Form2 和 Form3 时,我无法再次打开它们? Can anyone help me with this.谁能帮我这个。 Many thanks.非常感谢。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace App51
{
  public partial class Form1 : Form
  {
    Form2 frm2 = Application.OpenForms["Form2"] as Form2;
    Form3 frm3 = Application.OpenForms["Form3"] as Form3;


public Form1()
{
  InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
  this.WindowState = FormWindowState.Maximized;
  IsMdiContainer = true;
  foreach (Control control in this.Controls)
    if (control is MdiClient)
    {
      control.BackColor = Color.DeepSkyBlue;
      break;
    }
}
  

private void abrirForm2ToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (frm2 != null)
  {
    frm2.WindowState = FormWindowState.Normal;
    frm2.BringToFront();
    frm2.Activate();
  }
  else
  {
    frm2 = new Form2();
    frm2.MdiParent = this;
    frm2.Show();
  }
}


private void abrirForm3ToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (frm3 != null)
  {
    frm3.WindowState = FormWindowState.Normal;
    frm3.BringToFront();
    frm3.Activate();
  }
  else
  {
    frm3 = new Form3();
    frm3.MdiParent = this;
    frm3.Show();
  }
}

} } } }

I changed the code a bit.我稍微更改了代码。 Now it works:) What do you think of it?现在它起作用了:) 你怎么看?

using System;
using System.Drawing;
using System.Windows.Forms;

namespace App51
{
  public partial class Form1 : Form
  {
    Form2 frm2 = new Form2();
    Form3 frm3 = new Form3();


public Form1()
{
  InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
  this.WindowState = FormWindowState.Maximized;
  IsMdiContainer = true;
  foreach (Control control in this.Controls)
    if (control is MdiClient)
    {
      control.BackColor = Color.DeepSkyBlue;
      break;
    }
}
  

private void abrirForm2ToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (frm2 != null)
  {
    if (frm2.Visible == true)
    {
      frm2.WindowState = FormWindowState.Normal;
      frm2.BringToFront();
      frm2.Activate();
    }
    else
    {
      frm2 = new Form2();
      frm2.MdiParent = this;
      frm2.Show();
    }
  }
}
  

private void abrirForm3ToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (frm3 != null)
  {
    if (frm3.Visible == true)
    {
      frm3.WindowState = FormWindowState.Normal;
      frm3.BringToFront();
      frm3.Activate();
    }
    else
    {
      frm3 = new Form3();
      frm3.MdiParent = this;
      frm3.Show();
    }
  }
}

} } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM