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

2023-09-03 06:13:07 作者:중독 (上瘾)

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

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

Form1中我有检查数据库发送的任何新的消息,一个计时器。当一个新的消息发现,该聊天的形式出现显示该消息。

In Form1 I 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.

到目前为止,一切工作正常,但我只有一个问题。

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.

我正在使用的code:

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();
                    }

 }

我试图取代线: frm14.Show(); frm14.ShowDialog();

我注意到,在接收到新的消息时,在聊天窗口(form14)示出,并且当来自同一用户接收到另一消息时,没有新的聊天窗口出现,但问题是,i之后关闭聊天窗口,甚至当我收到新邮件不会再出现。

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.Text)更改为用户的全名,而在收到的消息时,我检查了具体的窗口是否已经打开,那么就不要打开它,否则我显示了使用 .Show()法的形式。

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.

这是正确的方式,使窗口不会出现如果收到,它是aloready开辟了新的消息?以及如何检查阉一个窗口是根据它的文本(标题栏文本)开?

感谢的抽出时间来阅读我的问题。任何帮助将是非常美联社preciated

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

推荐答案

首先要创建 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();
}