如何检查表格已打开 [英] How to check form is already open

查看:74
本文介绍了如何检查表格已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查表单是否已经打开.
如果它是打开的,则将其关闭并打开新的人.

这是我正在使用的代码,在上述情况下需要重新排列:

I want to check that form is already open or not.
If it is open, then close it and open new someone.

Here is the code, that I''m using and want to rearrange in above case:

SearchFiles searchedFiles = new SearchFiles();
searchedFiles.Owner = App.Current.Windows[0];
searchedFiles.ShowDialog();
searchedFiles.Close();



谢谢



Thanks

推荐答案

有点奇怪:没有开放"形式的概念.您只能检查它是否为Visible(具有这种布尔属性).我不确定您是否需要检查它.请继续阅读.

您创建一个窗体(带有构造函数),显示它,然后关闭.如果表单是关闭的(不是隐藏的,通常是通过处理FormClosing截获用户关闭"事件,取消事件并隐藏)来通过非主表单通过非主表单完成的),但实际上是关闭的,被处理掉,所以您不能再次打开它,但是可以读取它的属性.

因此,您还可以处理事件FormClosingFormClosed,使其关闭并在其中设置一些标志,以便以后进行测试.

我认为这不是您真正需要的.您实际上不需要知道表单的状态.您只需要使用其用法的适当模式即可.

如果使用仅使用ShowDialog的表单,则不应调用Close. 只需删除调用Close的行即可; 这样,您可以简单地多次调用ShowDialog.这是最好的方式显示形式的方式,因为如果要保留其状态.

如果要使用Show将表单显示为非模式形式,或者需要将其与ShowDialog混合使用(出于某些奇怪的原因),最好的模式是取消FormClosing(事件自变量参数的成员为Cancel如果关闭原因为System.Windows.Forms.CloseReason.UserClosing,则应将其分配为true.请参见:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.formclosingeventargs.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.closereason.aspx [ ^ ].

另外(甚至更好,因为表单类仍然是System.Window.Forms.Form的子类),请覆盖虚拟方法OnFormClosing:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx [ ^ ].

-SA
It''s a bit weird stuff: there is no such concept as "open" form. You can only check if it is Visible (there is such Boolean property) or not. I''m not sure you need to check it though. Please keep reading.

You create a form (with a constructor), show it and then close. If the form is closed (not hidden, which is most usually done with non-main forms through interception of "close by the user" event by handling FormClosing, cancellation the event and hiding), but really closed, it gets disposed, so then you cannot open it again, but can read its properties.

So, you could also handle the event FormClosing, or FormClosed, let it be closed and set some flag in it which you can test later.

I don''t think this is what you really need. You don''t really need to know the status of the form. You just need to use appropriate pattern of its usage.

If you use a form using only ShowDialog, you should not call Close. Just remove the line with the call to Close; and it will work just fine. This way, you can simply call ShowDialog any number of times. This is the best way of showing a form in a modal way, because if will keep its status.

If you want to show form as non-modal using Show, or you need to mix it with ShowDialog (by some weird reason), the best pattern is cancellation of FormClosing (the event argument parameter has the member Cancel you should assign to true if closing reason is System.Windows.Forms.CloseReason.UserClosing. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx[^].

Alternatively (and even better, because your form class subclasses System.Window.Forms.Form anyway), override the virtual method OnFormClosing:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx[^].

—SA


如果您使用的是ShowDialog,则无需关闭表单-ShowDialog直到关闭该表单才返回. > 因此,如果使用ShowDialog,则无论如何都不会获得该窗体的两个实例...

如果改用Show,它将立即返回,并且表单将独立显示.在这种情况下,您可以获得两个实例.

我要做的是保留一个类级别的SearchFiles变量,最初为null,并处理FormClosed事件:

If you are using ShowDialog, then you do not need to Close the form - ShowDialog will not return until the form is closed anyway.
So, if you use ShowDialog, you won''t get two instances of the form anyway...

If you use Show instead, it will return immediately, and the form will display independently. In this case you could get two instances.

What I would do is keep a class level SearchFiles variable, initially null, and handle the FormClosed event:

if (mySearchFiles != null)
   {
   mySearchFiles.Close();
   mySearchFiles = null;
   }
mySearchFiles = new SearchFiles();
mySearchFiles.FormClosed += new FormClosedEventHandler(mySearchFiles_FormClosed);
mySearchFiles.Show();

...

void mySearchFiles_FormClosed(object sender, FormClosedEventArgs e)
   {
   mySearchFiles = null;
   }


嗨...

我创建了一个静态类,在其中保留所有公共静态标志变量,以便在任何窗体打开时将变量的标志设置为true,而在窗体关闭时将其取消标志的设置为false.

通过这种方式,我可以检查不同形式的变量,以查看变量是被标记为打开还是关闭,并且在打开或关闭的基础上,我可以要求第一种形式显示第二种形式,或者将其集中显示已经开放.

这可能是一种非常不安全的方式,可以公开地公开某些标志变量,但对我来说效果很好.

我相信会有其他更好的方法来做到这一点.随时欢迎其他解决方案和建议.

〜AK
Hi...

I have created a static class in which I keep all the public static flag variables so that when any form opens it flags of the variable to true and when the form is closing it unflags it to false.

In this way I can check the variables from different forms to see if the variable is flagged on or off and on that basis of either being on or off I can either ask the first form to show the second form or bring it to focus if it is already open.

This maybe a very unsecured way to expose certain flag variables publicly but works well for me.

I am sure there would be other better ways to do this. Always open to alternate solutions and suggestions.

~ AK


这篇关于如何检查表格已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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