防止显示一种形式的多个实例 [英] preventing multiple instance of one form from displaying

查看:49
本文介绍了防止显示一种形式的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种应用程序,其中有一种主要形式和可以同时使用的其他几种形式.当用户单击以打开另一个表单时,我要创建它,以便单击该表单的按钮不会再次打开该表单(如果已打开).

I am working on an application in which there is one main form and several other forms that can be used concurrently. when A user clicks to open another form, I'd like to make it so that clicking the button for the form does not open the form up again if it is already open.

showDialog将无法正常工作,因为用户仍然需要访问主窗体上的控件.

showDialog wont work because the user still needs to have access to the controls on the main form.

这是我在帮助窗口中的代码,所有其他表单都以相同的方式打开.

here is my code for the help window, all the other forms open the same way.

private void heToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form help = new help();
            help.Show();
        } 

推荐答案

使用单例:

class HelpForm : Form
{
   private static HelpForm _instance;
   public static HelpForm GetInstance()
   {
     if(_instance == null) _instance = new HelpForm();
     return _instance; 
   }
}

.......

private void heToolStripMenuItem_Click(object sender, EventArgs e)
{
     HelpForm form = HelpForm.GetInstance();
     if(!form.Visible)
     {
       form.Show();
     }
     else
     {
       form.BringToFront();
     }
}

这篇关于防止显示一种形式的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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