防止重复的 MDI 子表单
如果 MDI 容器中的某个表单已经打开,是否有办法阻止打开该表单?
Is there a way to prevent the opening of a certain form within an MDI container if that said form is already opened?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以对 OpenForms 集合进行交互以检查是否已经存在给定类型的表单:
You can interate over the OpenForms collection to check if there is already a form of the given type:
AFAIK 没有标准方法。您必须自己实施它。我会这样做:
如果关心线程安全,请添加适当的锁。
AFAIK there is no standard way. You'll have to implement it yourself. I'd do it this way:
If thread safety is of concern, add the appropriate
lock
s.这段代码工作
this code working
虽然这篇文章很旧,但我认为这会有所帮助。
如果表单也最小化,则需要处理。这是完整的示例:
Though this post is very old, I thought this will add a help.
Need to handle if form is Minimized too. Here is the complete example:
这段代码在 vb.net 中对我有用
This code work for me in vb.net
可以使用泛型(在 C# 和 VB.net 选项下方)实现方法,如果需要打开不同的 MDI 表单,这会很有用。
C#
按如下方式使用(在
multipleInstances
中指示false
以防止出现)VB.NET
按如下方式使用(为
bMultipleInstances 指示
以防止它们)False
)A method can be implemented using Generics (below C# and VB.net options), which can be useful if different MDI Forms need to be opened.
C#
Use it as follows (indicate
false
inmultipleInstances
to prevent them)VB.NET
Use it as follows (indicate
False
forbMultipleInstances
to prevent them)这段代码在 C# 中对我有用
This code work for me in C#