且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正确的方法来检查,如果一个表单已经显示出?

更新时间:2023-10-14 19:59:40

首先要创建 Form14 每次有新邮件时,一个新的实例。

Firstly you are creating a new instance of Form14 every time you have a new message.

其次显示的ShowDialog 做两件完全不同的事情:

Secondly Show and ShowDialog do two very different things:

显示中只是显示了形式,而的ShowDialog 的显示形式为一个模态对话框。这意味着用户不能做任何事情,直到他们解散的形式。

Show just displays the form, whereas ShowDialog displays the form as a modal dialog. This means the user can't do anything else until they dismiss the form.

您需要有形式的一个实例,您可以使用可见属性,以确定是否它的显示与否。所以,你将有:

You need to have a single instance of the form and you can use the Visible property to determine whether it's shown or not. So you would have:

private Form14 frm14;

然后在构造函数中:

Then in the constructor:

frm14 = new Form14();

然后在你的code:

Then in your code:

if (!frm14.Visible)
{
    // Add the message
    frm14.Show();
}