我有一些代码使用自定义popupNotifier订阅点击事件,如下所示:
popupNotifier1.Click += new EventHandler(PopUpClicked);
这意味着当有人点击打开弹出窗口时会启动一个url字符串。假设_url
暂时是全球性的。
我在PopUpClicked中执行此操作:
public void PopUpClicked(object sender, EventArgs e)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(_url);
proc.StartInfo = startInfo;
proc.Start();
}
网址_url
包含一个字符串,如:
http://mysite/mypage.aspx?MyID=100
这很好用..打开页面但我注意到它会打开同一页面的多个标签。 我不明白为什么?
只是为了添加更多代码,我从每分钟命中的计时器事件中调用此代码,但注意if条件,它只在有数据的情况下订阅事件:
private void timer1_Tick(object sender, EventArgs e)
{
SqlDataReader sr;
string ticketInfo = String.Empty;
string url = String.Empty;
bool hasData = false;
using (SqlConnection sc = new SqlConnection(_connString))
{
using (SqlCommand scmd = new SqlCommand("select_poll"))
{
scmd.Connection = sc;
scmd.CommandType = CommandType.StoredProcedure;
scmd.Parameters.Add("LoginID", SqlDbType.BigInt).Value = _userLoginID;
scmd.Parameters.Add("FacilityID", SqlDbType.BigInt).Value = _userFacilityID;
sc.Open();
sr = scmd.ExecuteReader(CommandBehavior.CloseConnection);
if (sr != null)
{
while (sr.Read())
{
hasData = true;
ticketInfo += sr["TicketID"].ToString() + " - " + sr["Ticket"].ToString() + Environment.NewLine;
_url = "http://mysite/mypage.aspx?ID=" + sr["TicketID"].ToString();
}
}
}
}
if (hasData)
{
popupNotifier1.TitleColor = System.Drawing.Color.Green;
popupNotifier1.ContentText = ticketInfo;
popupNotifier1.Scroll = true;
popupNotifier1.TitlePadding = new System.Windows.Forms.Padding(2);
popupNotifier1.ContentPadding = new System.Windows.Forms.Padding(2);
popupNotifier1.Image = Properties.Resources.medical;
popupNotifier1.ImagePadding = new System.Windows.Forms.Padding(10);
popupNotifier1.Click += new EventHandler(PopUpClicked);
popupNotifier1.Popup();
}
}
public void PopUpClicked(object sender, EventArgs e)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(_url);
proc.StartInfo = startInfo;
proc.Start();
}
答案 0 :(得分:0)
popupNotifier1.Click += new EventHandler(PopUpClicked);
以上几行多次订阅event
,因此第一次启动PopUp
时会触发one tab
,下次2 tab
,然后下次three tab
n等等
只添加处理程序一次,最好的方法是,在你启用代码中的计时器之前
像
popupNotifier1.Click += new EventHandler(PopUpClicked);
timer1.Enabled = true;
private void timer1_Tick(object sender, EventArgs e)
{
SqlDataReader sr;
string ticketInfo = String.Empty;
string url = String.Empty;
bool hasData = false;
using (SqlConnection sc = new SqlConnection(_connString))
{
using (SqlCommand scmd = new SqlCommand("select_poll"))
{
scmd.Connection = sc;
scmd.CommandType = CommandType.StoredProcedure;
scmd.Parameters.Add("LoginID", SqlDbType.BigInt).Value = _userLoginID;
scmd.Parameters.Add("FacilityID", SqlDbType.BigInt).Value = _userFacilityID;
sc.Open();
sr = scmd.ExecuteReader(CommandBehavior.CloseConnection);
if (sr != null)
{
while (sr.Read())
{
hasData = true;
ticketInfo += sr["TicketID"].ToString() + " - " + sr["Ticket"].ToString() + Environment.NewLine;
_url = "http://mysite/mypage.aspx?ID=" + sr["TicketID"].ToString();
}
}
}
}
if (hasData)
{
popupNotifier1.TitleColor = System.Drawing.Color.Green;
popupNotifier1.ContentText = ticketInfo;
popupNotifier1.Scroll = true;
popupNotifier1.TitlePadding = new System.Windows.Forms.Padding(2);
popupNotifier1.ContentPadding = new System.Windows.Forms.Padding(2);
popupNotifier1.Image = Properties.Resources.medical;
popupNotifier1.ImagePadding = new System.Windows.Forms.Padding(10);
popupNotifier1.Popup();
}
}
public void PopUpClicked(object sender, EventArgs e)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(_url);
proc.StartInfo = startInfo;
proc.Start();
}
答案 1 :(得分:0)
您多次订阅该活动。在这种特殊情况下,只要在计时器触发时满足特定条件,您就可以订阅该事件。这似乎不止一次发生。
您几乎肯定希望在tick事件之外附加事件处理程序,可能是在首次加载表单时。