C#如何检查表单是否已在紧凑框架中打开
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
//iterate through
}
或者,
Form fc = Application.OpenForms["FORMNAME"]; if (fc != null) fc.Close(); fm.Show();
但这在紧凑框架 3.5 中不起作用。我如何检查表单是否已在 CF 3.5 中打开?
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
//iterate through
}
OR
Form fc = Application.OpenForms["FORMNAME"]; if (fc != null) fc.Close(); fm.Show();
but non of this works in compact framework 3.5. How can i check if form is already opened in CF 3.5?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如@Barry 所写,你必须自己做。最简单的方法是使用字典。您的密钥可以是表单的类型、名称或您需要的任何内容。
或者,如果您想支持多种表单类型并希望避免强制转换,请使用泛型方法:
有两种泛型重载。其中之一的使用方式如下:
或者,如果您需要在初始化期间将参数传递给表单:
As @Barry wrote, you will have to do it yourself. Easiest way is to use a dictionary. Your key can be a form's type, its name, or whatever you need.
Or, if you want to support multiple form types and want to avoid casting, use a generic method:
There are two generic overloads. One of them is used like this:
or, if you need to pass parameters to your form during init:
Compact Framework 中不存在
Application.OpenForms
集合。您必须推出自己的收藏并以这种方式跟踪它们。
这里有很好的教程解释了如何实现这一点。
The
Application.OpenForms
collection doesn't exist in the Compact Framework.You would have to roll your own collection and keep track of them that way.
There is nice tutorial here that explains how to achieve this.
您可以在所有表单上都有一个静态布尔字段。然后,您必须创建几个方法来切换它,并将 Opening 和 Closing 事件处理程序绑定到它。诚然,这不是最优雅的解决方案。
或者在某个地方创建一个表单集合,每次加载表单时都会在其中存储对其自身的引用,并对其进行迭代以检查表单是否打开/存在
You could have a static boolean field on all your forms. Then you'd have to create a couple methods to toggle it, and bind the Opening and Closing event handlers to it. Not the most elegant solution admittedly.
Either that or make a collection of forms somewhere, and each time a form loads make it store a reference to itself in it, and iterate against that to check if the form is open/exists