Application.OpenForms.Count = 0总是 [英] Application.OpenForms.Count = 0 always

查看:480
本文介绍了Application.OpenForms.Count = 0总是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况。 Application.OpenForms犯规返回正确的结果。即 Application.OpenForms.Count = 0 总是..

I have this situation. Application.OpenForms doesnt return the right result. ie Application.OpenForms.Count = 0 always..

获取形式的目的是获取表单的主人,这样我可以通过所有者的的MessageBox.show()函数的参数。

Purpose of getting the form is get the owner of the Form so that I can pass the owner as the parameter of the MessageBox.Show() function.

推荐答案

有一个在Windows窗体,使得从Application.OpenForms集合形式消失的错误。这会在你的创建窗口后,分配ShowInTaskbar,FormBorderStyle,最小/ MaximizedBox,RightToLeftLayout,帮助按钮,不透明度,TransparencyKey,ShowIcon或MdiParent属性的发生。这些属性是特殊的,因为它们被指定为天然CreateWindowEx函数()调用风格的标志。此示例的形式演示了该bug:

There's a bug in Windows Forms that makes a form disappear from the Application.OpenForms collection. This will happen when you assign the ShowInTaskbar, FormBorderStyle, Min/MaximizedBox, RightToLeftLayout, HelpButton, Opacity, TransparencyKey, ShowIcon or MdiParent property after the window was created. These properties are special in that they are specified as style flags in the native CreateWindowEx() call. This sample form demonstrates the bug:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        button1.Click += button1_Click;
    }
    private void button1_Click(object sender, EventArgs e) {
        Console.WriteLine(Application.OpenForms.Count);
        this.ShowInTaskbar = !this.ShowInTaskbar;
        Console.WriteLine(Application.OpenForms.Count);
    }
}

Windows窗体必须调用CreateWindowEx函数()再次做出更改的属性有效,通过不同风格的标志。首先破坏原有的窗口有副作用超出了非常明显的闪烁,其中之一是,应用程序类失去跟踪的形式,因为它看到窗口消失。有了它创建的新窗口时不加回的错误。通过设置属性只有在运行之前CreateWindowEx函数()被调用时,没有任何事件处理程序的构造函数,code避免故障。

Windows Forms must call CreateWindowEx() again to make the changed property effective, passing different style flags. Destroying the original window first has side effects beyond the very noticeable flicker, one of them is that the Application class loses track of the form since it sees the window disappear. With the bug that it doesn't add it back when the new window is created. Avoid the bug by setting the property only in the constructor, code that runs before CreateWindowEx() is called, not in any event handlers.

在一般情况下,避免依赖,由于这个bug OpenForms。给需要显示消息框,通过它的构造函数的引用,表单实例类。消息框通常计算出父窗口本身正确顺便说一句,它会选择活动窗口,这就是时是正确的99%。如果你需要它来调用BeginInvoke()从工作线程那么一定要复制SynchronizationContext.Current在构造函数和以后调用它的post()方法。确保您的图书馆也将与其他GUI类库的工作。

In general, avoid relying on OpenForms due to this bug. Give the class that needs to display the message box a reference to the form instance through its constructor. MessageBox usually figures out a parent window by itself correctly btw, it will pick the active window and that's correct 99% of the time. If you need it to call BeginInvoke() from a worker thread then be sure to copy SynchronizationContext.Current in your constructor and call its Post() method later. Ensures your library will also work with other GUI class libraries.

这篇关于Application.OpenForms.Count = 0总是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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