使用计时器打开窗体? [英] Opening Window form using timer?

查看:82
本文介绍了使用计时器打开窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

流程:

我有桌子

s.no ID_1 ID_2内容

1 1001 4001嗨

2 1002 4002你好



我用计时器在windows格式的gridview中绑定这个表

使用相同的计时器我检查是否存在是表格中_ID2的任何内容。

如果内容值为true,则应显示新的窗口表格。



面向问题:

每次定时器加载时它都会显示新的窗口,即使它已经打开同样的_ID已经如何检查表格是否已经打开_ID已打开?





我的代码:

private void timer1_Tick(object sender,EventArgs e)

{

试试

{

string Msg = string.Empty;

int Suc;

string tmpSuc = string.Empty;

string i;

string Role = Form1.role.ToString()。Trim();

XmlNode节点;

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节点)

{

DataRow dr = dt.NewRow();

dr [Students_Name] = node [Name]。InnerText;

Student_Name = Convert.ToString(dr [Students_Name]);

dr [ID] = node [ID]。InnerText;

tmpSid = Convert.ToIn t32(dr [ID]);

string chtdtm =MM-d-yyyy;

字符串日期;

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

}

}





//聊天= tmpChat;

Student_ID = tmpSid;

i = node [Status] .InnerText;

if(i == true)

{

博士[状态] =在线;

}

else

{

dr [Status] =离线;

}

dt.Rows.Add (博士);

}

dataGridView1.DataSource = dt;

}

catch(例外)

{



throw;

}



}

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

}

推荐答案

我不会使用OpenForms集合,而是保存一个类级别的Form / ID值字典:

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



当我创建要显示的新表单实例时,我会将其添加到字典中,并为其添加一个处理程序'FormClosed事件以删除它。



然后,当你想要显示一个时,你可以查看字典以查看它是否已经存在,如果没有则添加它,或者带它如果确实存在,则为前景。


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.


这篇关于使用计时器打开窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆