01
2012
04

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

  方法一:
  直接检测是否已经打开此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()); }
  }
« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。