如何检查窗体是否已打开,如果已打开则关闭它? [英] How to check if a windows form is already open, and close it if it is?

查看:26
本文介绍了如何检查窗体是否已打开,如果已打开则关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单 "fm",它是一个简单的信息窗口,每 10 分钟打开一次 (fm.Show();).

I have a form "fm" that is a simple info window that opens every 10 mins (fm.Show();).

我如何做到每 10 分钟检查一次 "fm" 表单是否打开,如果打开它关闭它并再次打开它!

How I can make that every 10 mins it will check if the form "fm" is open and if it is open it closes it and open it again!

现在表单 fm 总是使用 form fm = new form();
所以当我尝试检查表单是否打开时,它总是错误的,即使之前有一个表单,它也会打开一个新窗口!

Now the form fm is always created with form fm = new form();
so when I try to check if the form is open it will always be false and open a new window even if there is one form before!

我需要一个工具给它一个唯一标识,然后检查这个具有唯一标识的表单是否被打开!

I need to have a tool to give it a unique identity and then check if this form with unique identity is opened or not!

我不想只更新表单 (fm) 上的数据,因为我有一个带有按钮的复杂信息.

I do not want to just update the data on the form (fm), because I have a complicated info with buttons.

表单名称为"UpdateWindow"

谢谢

推荐答案

也许这有帮助:

FormCollection fc = Application.OpenForms;

foreach (Form frm in fc)
{
//iterate through
     if (frm.Name == "YourFormName")
     {
         bFormNameOpen = true;
     }
}

foreach 中的一些代码来检测特定的表单,它可以完成.虽然未经测试.

Some code in the foreach to detect the specific form and it could be done. Untested though.

http://bytes.com/上找到topic/c-sharp/answers/591308-iterating-all-open-forms

这篇关于如何检查窗体是否已打开,如果已打开则关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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