MDI子打开和关闭 [英] MDI Child open and close

查看:86
本文介绍了MDI子打开和关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户尝试打开已经在MDIParent中打开的子窗体时,我想避免我的子窗体多次出现.相同的子窗体打开,我可以看到两个子窗体都在MDI中.以下代码解决了这个问题
公共局部类主页:表格
{
ShopMaster shopmaster = null;
私有void shopMasterToolStripMenuItem_Click(对象发送者,EventArgs e)
{

如果(shopmaster == null)
{
shopmaster = new ShopMaster();
shopmaster.MdiParent = this;
shopmaster.Show();
}
}
}
但是如果我关闭这个孩子,它将再也无法打开.所以我只想如果child1是打开的,那么它不应该再打开,直到它关闭.关闭它后,我们可以再次打开它.

I want to avoid my child form from appearing many times when a user tries to open the child form which is already open in MDIParent. the same Child Form Opens and I can see two child form are in MDI.The following code solve this problem
public partial class Home : Form
{
ShopMaster shopmaster=null;
private void shopMasterToolStripMenuItem_Click(object sender, EventArgs e)
{

if (shopmaster == null)
{
shopmaster = new ShopMaster();
shopmaster.MdiParent = this;
shopmaster.Show();
}
}
}
but if i close this child it never open again. so i just want that if child1 is open then it should not open again untill it get close. After closing it we can open it again.

推荐答案

将变量设为静态

Make your variable as static

public static ShopMaster shopmaster=null;



然后在ShopMaster的FormClosing事件中编写代码



then write the code in FormClosing event of your ShopMaster

private void ShopMaster_FormClosing(object sender, FormClosingEventArgs e)
      {
          string ans = (MessageBox.Show("Are you sure to Exit!", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)).ToString();
          if (ans == "Yes")
          {
              Home.shopmaster=null;
          }
          else
          {
              e.Cancel = true;
          }

      }


这篇关于MDI子打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆