C# 检查表单是否已显示的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14920434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:27:26  来源:igfitidea点击:

The proper way to check if a form is already shown?

c#.netwinforms

提问by Rafik Bari

I have created a task managment application and I wanted to implement the ability for 2-users to chat about specific task.

我创建了一个任务管理应用程序,我想实现 2 用户聊天特定任务的能力。

In Form1I have a timer that check on the database for any new message sent. When a new message found, the chat form appear showing the message.

Form1我有一个计时器,用于检查数据库中是否有发送的任何新消息。找到新消息时,聊天表单会显示该消息。

Till now, everything is working as expected but I have only one problem.

到目前为止,一切都按预期工作,但我只有一个问题。

The problem :Once a new message found for the first time, the chat window appear, but when another new message is found, another window appear, and for each new message I have a new instance of the chat window created.

问题:第一次找到新消息后,聊天窗口就会出现,但是当发现另一条新消息时,会出现另一个窗口,并且对于每条新消息,我都会创建一个新的聊天窗口实例。

The code I'm using :

我正在使用的代码:

 List<string> tasksToDiscuss = checkForTasksToDiscuss(fullname);

        if (tasksToDiscuss.Count > 0) { 
 // open the chat window directly minimized
 Form14 frm14 = new Form14();
 frm14.get_from = fullname;
 frm14.get_to = tasksToDiscuss[1];
 frm14.get_task_id = int.Parse(tasksToDiscuss[3]);
 // set message as read
 if (setMessageAsRead(tasksToDiscuss[1], fullname, int.Parse(tasksToDiscuss[3])))
                    {
                        // now show the chat window minimized
                        frm14.Show();
                    }

 }

I tried to replace the line:
frm14.Show();with frm14.ShowDialog();

我试图替换该行:
frm14.Show();frm14.ShowDialog();

I noticed that when the new message is received, the chat window (form14) is shown, and when another message is received from the same user, no new chat window appear, but the problem is that after i close the chat window, it doesn't appear anymore even when i receive new messages.

我注意到当收到新消息时,显示聊天窗口(form14),当收到来自同一用户的另一条消息时,没有出现新的聊天窗口,但问题是我关闭聊天窗口后,它没有即使我收到新消息,也不再出现。

What I think to do is to change the chat window (Form14.Text) to the user fullname, and the next time a message is received, I check whether the specific window is already open, then don't open it otherwise i show the form using the .Show()method.

我认为要做的是将聊天窗口(Form14.Text)更改为用户全名,下次收到消息时,我检查特定窗口是否已经打开,然后不要打开它,否则我会显示使用.Show()方法形成。

Is this the proper way to make the window doesn't appear if a new message is received and it is aloready opened ? and How to check wether a window is opened according to it's Text (title bar text) ?

如果收到新消息并且它已经打开,这是使窗口不出现的正确方法吗?以及如何根据它的文本(标题栏文本)检查窗口是否打开?

Thank's for taking time reading my question. Any help would be highly appreciated

感谢您花时间阅读我的问题。任何帮助将不胜感激

采纳答案by ChrisF

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

首先,您Form14每次收到新消息时都会创建一个新实例。

Secondly Showand ShowDialogdo two very different things:

其次ShowShowDialog做两件非常不同的事情:

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

Show只显示表单,而ShowDialog将表单显示为模式对话框。这意味着用户在关闭表单之前不能做任何其他事情。

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

您需要有一个表单实例,您可以使用该Visible属性来确定它是否显示。所以你会有:

private Form14 frm14;

Then in the constructor:

然后在构造函数中:

frm14 = new Form14();

Then in your code:

然后在你的代码中:

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

回答by omer schleifer

Try making form14 a member of form1. When you get a new message check the Visible property of forom14 to know if it is already showing.

尝试使 form14 成为 form1 的成员。当您收到一条新消息时,请检查 forom14 的 Visible 属性以了解它是否已经显示。