您的位置:首页 > 编程语言 > C#

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

2013-04-29 23:48 141 查看
方法一:

  直接检测是否已经打开此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;

  }

该文章转载自无忧考网:http://www.51test.net
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# .NET