从winform应用程序打开网页会打开太多选项卡

本文关键字:太多 选项 网页 winform 应用程序 | 更新日期: 2023-09-27 17:58:13

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

从winform应用程序打开网页会打开太多选项卡

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

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

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