如何知道一个Form实例是否在运行? [英] How to know more than one instance of a Form is running or not?

查看:75
本文介绍了如何知道一个Form实例是否在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个带有1Menu项的Menustrip1说新话.

当我单击新建菜单项"时,假定Form1已打开.

我的问题是当我单击新建" 5次时.它正在打开Form1 5次.

即使我单击了N次,如何只打开一次Form1.

任何帮助将不胜感激.

问候,
Pawan.

Hi All,


I have a Menustrip1 With 1Menu Item Say New.

When i click on New Menu Item assume Form1 is opend.

My Problem is When i click on New for 5 Times. it is opening Form1 5 Times.

How to open Form1 Once only even though i clicked for N Times.

Any help would be really appreciated.

Regards,
Pawan.

推荐答案

您没有提到应用程序是SDI还是MDI,并且有不同的解决方法....

1)SDI:在具有新建"菜单项代码的窗体中的窗体级别上创建一个Form1 myForm = null;变量.单击菜单项后,您可以执行以下操作:

You don''t mention if you application is SDI or MDI, and there are different ways of solving this....

1) SDI: Create a Form1 myForm = null; variable on form level in the form where you have the New menu item code. When the menu item is clicked, you do:

if (myForm == null)
{
   myForm = new Form1();
   myForm.Show();
}



2)SDI:您可以使用布尔变量来跟踪Form1,当您打开Form1时,您可以设置form1IsOpen = true;.但是然后您需要在窗体中关闭窗体时从菜单中引发一个事件,以便可以将变量重置为false.

3)MDI:您需要遍历打开的MDI子窗体,并检查它们的类是否为Form1.



2) SDI: You can use a boolean variable to keep track of Form1, when you open it, you set form1IsOpen = true;. But then you need to raise an event in the form where you have your menu from Form1 when it''s closing so that you can reset the variable to false.

3) MDI: You need to loop through the open MDI child forms and check if their class is Form1.



另一个选择是以始终指向其对象的形式定义Shared(Static)属性.
我们可以包装逻辑
Hi,
Another option it to Define a Shared(Static) property in the form which always to points its object.
we can wrap the logic
if (myForm == null)
{
   myForm = new Form1();
   myForm.Show();
}

与该构造函数一起使用

那是

with in this constructor

that is

Class Class1

Shared ReadOnly Property Instance
   Get
    IF _instace is nothing then 
     instace  =New Class1
    End if
   End 
End Class


请参阅我以前的解决方案之一:
Please refer to one of my previous solutions: how to open only one instance of class not more than that ?[^].

Worked before and will also work for you!

-MRB


这篇关于如何知道一个Form实例是否在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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