0

我有一些使用自定义 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,_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();
        }
4

2 回答 2

0
popupNotifier1.Click += new EventHandler(PopUpClicked);

上面的行订阅了event多次,所以当第一次,当PopUp被触发时,它打开one tab,下一次2 tab,然后下一次three tabn,依此类推

仅添加一次处理程序,最好的方法是在您在代码中启用计时器之前

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();
        }
于 2013-02-06T16:26:13.357 回答
0

您多次订阅该事件。在这种特殊情况下,当您的计时器触发时,只要满足特定条件,您就订阅了该事件。这似乎不止一次发生。

您几乎肯定希望将事件处理程序附加到 tick 事件之外,可能是在首次加载表单时。

于 2013-02-06T16:26:33.257 回答