I have created a task managment application and I wanted to implement the ability for 2-users to chat about specific task.
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.
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();
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.
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.
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
Just call Show method on the instance. You can check Form. Visible property to check if the form open at the moment.
You can use Form. IsActive property.
Load Event (System. Windows. Forms) Occurs before a form is displayed for the first time.
To hide a form it is necessary to call the Hide() method of the form to be hidden.
Firstly you are creating a new instance of Form14
every time you have a new message.
Secondly Show
and ShowDialog
do two very different things:
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();
Then in your code:
if (!frm14.Visible)
{
// Add the message
frm14.Show();
} else{
// Top
frm14.BringToFront();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With