发生任何事件时,通知图标会自我复制

时间:2019-02-13 19:54:19

标签: c# .net winforms visual-studio-2017

我的Winforms表单中有一个通知图标,看来当发生任何类型的事件时,托盘图标都会重复。 我已经调试了其中一个问题,因为使用该对话框后关闭该对话框时会重复该问题。

它发生在调试和发布时。

另一个问题是使用运行方法的计时器。

我不明白为什么会这样。我的计时器昨晚运行了60次,每次运行有四种方法,并且托盘中有数百个图标。

我的代码如下:

         public Form1()
          {
                    InitializeComponent();

                    notifyIcon1.BalloonTipText = "Mappi CSV Manager is running.";
                    notifyIcon1.BalloonTipTitle = "Mappi CSV Manager";
                    notifyIcon1.Text = "Mappi CSV Manager";
          }

           private void Form1_Resize(object sender, EventArgs e)
           {
                    if (WindowState == FormWindowState.Minimized)
                    {
                        ShowIcon = false;
                        ShowInTaskbar = false;
                        notifyIcon1.Visible = true;
                        notifyIcon1.ShowBalloonTip(1000);
                    }
                }

                private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
                {
                    ShowInTaskbar = true;
                    notifyIcon1.Visible = false;
                    WindowState = FormWindowState.Normal;

                }

                  protected override void OnClosed(EventArgs e)
                {
                    base.OnClosed(e);
                    // Call Dispose to remove the icon out of notification area of Taskbar. 
                    notifyIcon1.Dispose();

                }

     protected override void OnFormClosing(FormClosingEventArgs e)
            {
                if (CloseCancel() == false)
                {
                    e.Cancel = true;
                };
            }

     //When closing the form
        public static bool CloseCancel()
        {
            const string message = "If you close the program, no files will be generated!";
            const string caption = "Stop!";
            var result = MessageBox.Show(message, caption,
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
                return true;
            else
                return false;
        }

  //Set new value for timer
        private void UdTimerValue_ValueChanged(object sender, EventArgs e)
        {
            timer1.Interval = Convert.ToInt32(udTimerValue.Value) * 60000;
        }

        //Start generating CSV's
        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                if (AutoGenerateEnabled)
                {
                    richLogWindow.AppendText("CSV Created at: " + DateTime.Now + "\r\n");
                    var startdate = "";

                    if(DateTime.Now.Hour == 1)
                    {
                        richLogWindow.Clear();
                        startdate = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
                        CSVGenerator.GenerateCSV(startdate, this);
                    }
                    else
                    {                        
                        startdate = DateTime.Today.ToString("yyyy-MM-dd");
                        CSVGenerator.GenerateCSV(startdate, this);                        
                    }                    
                }
                else
                {
                    return;
                }
            }
        }

为什么每次单击按钮或发生事件时,此代码都会产生另一个任务栏图标。

TIA

1 个答案:

答案 0 :(得分:1)

我发现了错误。我已将RichTextBoxAppend.AddNewText("test me", new Form1());放置在每次运行流程时创建的新表单。我是个白痴!