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

イベントに複数回サブスクライブしています。この特定のケースでは、タイマーが起動したときに特定の条件が満たされるたびに、イベントをサブスクライブしています。これは複数回発生するようです。

ほとんどの場合、フォームが最初にロードされるときに、イベント ハンドラーを tick イベントの外側にアタッチします。

于 2013-02-06T16:26:33.257 に答える
0
popupNotifier1.Click += new EventHandler(PopUpClicked);

上記の行は複数回サブスクライブするeventため、最初に起動したときPopUpに開きone tab、次回2 tab、次にthree tabn というように続きます。

コードでタイマーを有効にする直前に、ハンドラーを 1 回だけ追加するのが最善の方法です。

お気に入り

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 に答える