且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows窗体问题[Singleton]

更新时间:2023-12-06 08:34:28

为什么不仅将表单创建为成员变量,因此您将只能显示一个?如果要跟踪多少,只需将静态变量放入递增的形式.缺乏确定性的销毁意味着它是最可靠的手动抬起和放下的方法,尽管您可能还是会挂起一个事件来为您做,例如当表格可见状态时更改为不可见.
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[^]