Windows窗体问题[Singleton] [英] Windows Form Problem [Singleton]

查看:58
本文介绍了Windows窗体问题[Singleton]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一下在我的项目中打开特定表单的次数,因此只打开了一个表单.

I''m wanting to check to see how many times a particular form is opened in my project, so that only one form is open.

推荐答案

为什么不仅将表单创建为成员变量,因此您将只能显示一个?如果要跟踪多少,只需将静态变量放入递增的形式.缺乏确定性的销毁意味着它是最可靠的手动抬起和放下的方法,尽管您可能还是会挂起一个事件来为您做,例如当表格可见状态时更改为不可见.
Why not just create your form as a member variable, and thus you''ll only ever be able to show one ? If you want to track how many, just put a static variable in the form that gets incremented. The lack of deterministic destruction means it''s most reliable to make it go up and down by hand, although there''s probably an event you can hook in to to do it for you, still, such as when the forms visible status changes to not visible.


添加到CG的响应中
---------------------------

一种流行的方法是将窗体的Closed 事件挂接到父窗体中,以便在关闭子窗体时,可以将member变量设置为null.因此,如果member变量为null,则意味着需要重新创建并显示该表单,否则就意味着该表单已经可见并且可能只需要聚焦/激活.
Adding to CG''s response
---------------------------

One popular approach is to hook the Closed event of the form in the parent form, so that when the child form''s closed, the member variable can be set to null. Thus if the member variable is null, it means the form needs to be recreated and reshown, else it means the form''s already visible and probably just needs to be focused/activated.


请参阅此有关单例的链接.我过去曾使用过这种模式,以确保我只有一个表单实例.

http://msdn.microsoft.com/en-us/library/ff650316.aspx [ ^ ]
See this link about singletons. I have used this pattern in the past to ensure I only have one instance of a form.

http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]


这篇关于Windows窗体问题[Singleton]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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