如何只打开一个不超过该类的实例? [英] how to open only one instance of class not more than that ?

查看:52
本文介绍了如何只打开一个不超过该类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好老年人,

这是我对社区的第一篇文章.

我是c#的新手,想知道是否可以仅打开一个表单实例?

我的意思是,如果表单已经打开,则应将其激活,而不是打开它的新实例.

提前谢谢.

[用C#替换了C ++标签-Rajesh]

Hello Seniors,

This is my first post to the community.

I am new to c# and want to know if its possible to open only one instance of a form?

I mean if the form is already open then it should be activated instead of opening new instance of it.

Thanks in advance.

[Replaced C++ tag with C# - Rajesh]

推荐答案

请为此使用单例模式.简短摘要:

将类构造函数设为私有.

具有公共静态函数,例如GetInstance(...).
如果不为null,函数将返回变量实例的内容
否则它将实例化该类并将其分配给变量实例.

修改:
对不起!您当然是对的,关闭表格将对其进行处理,因此无法再使用.因此,我添加了一个事件处理程序,该事件处理程序在您关闭窗体时仅将其隐藏(也可以通过调用窗体的Close()方法来工作):

Please use the singleton pattern for that. Short summary:

Make class constructor private.

Have a public static function like GetInstance(...).
Function will return content of variable instance if not null
else it will instantiate the class and assign that to the variable instance.

Modification:
Sorry! Your''re right of course, closing the form will dispose it and it can''t be used anymore. So I added an event handler that just hides the form when you close it (also works by calling Close() method of form):

namespace Singleton
{
    public partial class Form1 : Form
    {
        private static Form1 instance;
        private Form1()
        {
            InitializeComponent();
            Form1.instance = this;
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true; //This prevents form from really closing
            ((Form1)sender).Hide();
        }

        public static Form1 GetInstance()
        {
            if (instance == null)
                return new Form1();
            else
               return instance;
        }
    }
}



现在可以了.在另一种形式中,我有一个带有单击事件处理程序的按钮:



That works now. In another form I have a button with click event handler:

private void button1_Click(object sender, EventArgs e)
{
    Form1 form = Form1.GetInstance();
    form.Show();
}


现在,您可以根据需要多次关闭" Form1,当按下按钮时,它会再次出现,并且始终是同一实例.


干杯


曼弗雷德(Manfred)


Now you can "close" Form1 as often as you want and when button is pressed it reappears and it''s always the same instance.


Cheers


Manfred




您似乎没有进行Google搜索: http://forums.techpowerup.com/showthread.php? t = 34685 [^ ]
Hi,

Looks like you didn''t do a Google search: http://forums.techpowerup.com/showthread.php?t=34685[^]


您还可以创建无模式表单,并根据需要显示/隐藏它.

You could also create a modeless form, and show/hide it as necessary.

public class MyClass
{
    MyForm form = null;

    // to show the form
    private void ShowForm()
    {
        if (form == null)
        {
            form = new MyForm();
        }
        form.Show();
    }

    // to hide the form
    private void HideForm()
    {
        if (form != null)
        {
            form.Hide();
        }
    }
}


这篇关于如何只打开一个不超过该类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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