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

 方法一:

直接检测是否已经打开此MDI窗体

  1. // 是否已经打开了?(用循环来判断)
  2.             foreach (Form childrenForm in this.MdiChildren)
  3.             {
  4.                 //检测是不是当前子窗体名称
  5.                 if (childrenForm.Name == "子窗体名称")
  6.                 {
  7.                     //是的话就是把他显示
  8.                     childrenForm.Visible = true
  9.                     //并激活该窗体
  10.                     childrenForm.Activate();     
  11.                     return;
  12.                 }
  13.             }
  14.             //下面是打开子窗体
  15.             Form1 childrenForm = new Form1();
  16.             childrenForm.MdiParent = this
  17.             childrenForm.Show();
  18.             childrenForm.WindowState = FormWindowState.Maximized;

方法二:

将子窗体设成单件:

  1. namespace WindowsFile
  2. {
  3.     public partial class Form3 : Form
  4.     {
  5.         public Form3()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.         private void Form3_Load(object sender, EventArgs e)
  10.         {
  11.             richTextBox1.LoadFile(".//test.txt", RichTextBoxStreamType.PlainText);
  12.         }
  13.         private static Form3  childform=null;
  14.         public  static Form3 Form3Signleton()
  15.         {
  16.             if (childform == null)
  17.             {
  18.                 childform = new Form3();
  19.             }
  20.             return childform;
  21.         }
  22.         private void Form3_FormClosing(object sender, FormClosingEventArgs e)
  23.         {
  24.             try
  25.             {
  26.                 this.Dispose(true);
  27.                 childform  = null;
  28.             }
  29.             catch(Exception e)
  30.             { MessageBox(e.ToString()); }
  31.         }

方法三:

这个没用过,也是直接判断

  1. private static frm_rk frmchild;//定义子窗体 
  2. private void 商品入库ToolStripMenuItem_Click(object sender, EventArgs e)//按钮事件 
  3.         { 
  4.           frm_rk frmchild = GetForm(); 
  5.           frmchild.Show(); 
  6.         } 
  7.         private frm_rk GetForm()//判断子窗体状态,防止重复显示 
  8.         { 
  9.             if (frmchild == null || frmchild.IsDisposed) 
  10.             { 
  11.                 frmchild = new frm_rk(); 
  12.                 frmchild.MdiParent = this
  13.             } 
  14.             return frmchild; 
  15.         }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值