C#中避免相同MDI子窗口重复打开的方法

发布时间:2011-08-29 共1页

  方法一:
  直接检测是否已经打开此MDI窗体
  // 是否已经打开了?(用循环来判断)
  foreach (Form childrenForm in this.MdiChildren)
  {
  //检测是不是当前子窗体名称
  if (childrenForm.Name == "子窗体名称")
  {
  //是的话就是把他显示
  childrenForm.Visible = true;
  //并激活该窗体
  childrenForm.Activate();
  return;
  }
  }
  //下面是打开子窗体
  Form1 childrenForm = new Form1();
  childrenForm.MdiParent = this;
  childrenForm.Show();
  childrenForm.WindowState = FormWindowState.Maximized;
  方法二:
  将子窗体设成单件:
  namespace WindowsFile
  {
  public partial class Form3 : Form
  {
  public Form3()
  {
  InitializeComponent();
  }
  private void Form3_Load(object sender, EventArgs e)
  {
  richTextBox1.LoadFile(".\\test.txt", RichTextBoxStreamType.PlainText);
  }
  private static Form3 childform=null;
  public static Form3 Form3Signleton()
  {
  if (childform == null)
  {
  childform = new Form3();
  }
  return childform;
  }
  private void Form3_FormClosing(object sender, FormClosingEventArgs e)
  {
  try
  {
  this.Dispose(true);
  childform = null;
  }
  catch(Exception e)
  { MessageBox(e.ToString()); }
  }
  方法三:
  这个没用过,也是直接判断
  private static frm_rk frmchild;//定义子窗体
  private void 商品入库ToolStripMenuItem_Click(object sender, EventArgs e)//按钮事件
  {
  frm_rk frmchild = GetForm();
  frmchild.Show();
  }
  private frm_rk GetForm()//判断子窗体状态,防止重复显示
  {
  if (frmchild == null || frmchild.IsDisposed)
  {
  frmchild = new frm_rk();
  frmchild.MdiParent = this;
  }
  return frmchild;
  }

百分百考试网 考试宝典

立即免费试用