Click here to Skip to main content
15,887,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Process:
I am having a table
s.no ID_1 ID_2 content
1 1001 4001 Hi
2 1002 4002 Hello

I used timer to bind this table in gridview of windows form
using the same timer i checked whether there is any content for the _ID2 from the table.
If the content value is true a new window form should be displayed.

Issue facing:
Each and every time the timer gets load it displaying the new windows even it is opened for the same _ID already how to check the form is open for the _ID which is opened already?


My code:
private void timer1_Tick(object sender, EventArgs e)
{
try
{
string Msg = string.Empty;
int Suc;
string tmpSuc = string.Empty;
string i;
string Role = Form1.role.ToString().Trim();
XmlNode Node;
int tmpSid;
string tmpChat = string.Empty;
wsTestService.STUDENTS_ONLINE(Role, out Node, out Msg, out Suc);
DataTable dt = new DataTable();
dt.Columns.Add("Students_Name", typeof(string));
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Status", typeof(string));
foreach (XmlNode node in Node)
{
DataRow dr = dt.NewRow();
dr["Students_Name"] = node["Name"].InnerText;
Student_Name = Convert.ToString(dr["Students_Name"]);
dr["ID"] = node["ID"].InnerText;
tmpSid = Convert.ToInt32(dr["ID"]);
string chtdtm = "MM-d-yyyy";
string date;
wsTestService.CHAT_RECEIVE_MESSAGE(tmpSid, Tutor_ID, out tmpChat, out date, out tmpSuc);
DateTime today = DateTime.Today;
//chtdtm = Convert.ToDateTime(chtdtm);
string tmpdate;
tmpdate = today.ToString(chtdtm);
if (tmpChat != string.Empty)
{
if (tmpdate == date)
{
do
{
Chat_Box_Test objCTB = new Chat_Box_Test();
Chatting = tmpChat;
objCTB.Show();
} while (Student_ID == tmpSid);
}
}


//Chatting = tmpChat;
Student_ID = tmpSid;
i = node["Status"].InnerText;
if (i == "true")
{
dr["Status"] = "online";
}
else
{
dr["Status"] = "offline";
}
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
}
catch (Exception)
{

throw;
}

}
Posted
Updated 10-May-13 2:00am
v2
Comments
[no name] 10-May-13 8:05am    
You can check and see if your form is already open by checking the Application.OpenForms collection.
riodejenris14 10-May-13 8:06am    
pls explain me detailly! with code...

1 solution

Rather than use the OpenForms Collection, I would save a class level Dictionary of Form / ID values:
C#
private Dictionary<int, Chat_Box_Test> Showing = new Dictionary<int, Chat_Box_Test>();

When I create the new form instance to show, I would add it to the dictionary, and add a handler to it's FormClosed event to remove it.

Then, when you want to show one, you can just look in the dictionary to see if it already exists, and add it if not, or bring it to teh foreground if it does exist.
 
Share this answer
 
Comments
riodejenris14 10-May-13 8:28am    
Dude how to make it Please explain me more
Then, when you want to show one, you can just look in the dictionary to see if it already exists, and add it if not, or bring it to teh foreground if it does exist.
OriginalGriff 10-May-13 9:27am    
if (!myDictionary.ContainsKey(idYouArelookingFor))
{
// Create a new one.
}
else
{
myDictionary[idYouArelookingFor].BringToFront();
}
riodejenris14 13-May-13 9:51am    
it's not working!!!!
OriginalGriff 13-May-13 10:30am    
Possibly a contender for the "most uninformative problem report" 2013...perhaps some details as to what is / isn't happening might help?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900