NotifyIcon BalloonTipClicked事件未触发

时间:2015-08-29 00:35:23

标签: c# events delegates notifyicon

您好我试图在用户点击气球提示时运行一些代码。目前它显示了气球提示,但当我点击它时代表没有被解雇。

这是创建NotifyIcon的地方

    private NotifyIcon notifyIcon;

    public ClockOutTimer(DateTime? clockOutTime)
    {
        //IF CLOCK OUT TIME = NULL SET CLOCK OUT TIME TO 5 HOURS IN FUTURE
        ClockOutTime = clockOutTime ?? DateTime.Now.AddHours(5);
        notifyIcon = new NotifyIcon()
            {
                Icon = Properties.Resources.MainIcon,
                Text = "Organiser"
            };

        //CREATE DELEGATE FOR BALOON CLICKED
        notifyIcon.BalloonTipClicked += delegate
        {
            //NOT GOING HERE :(
            MessageBox.Show("TEST");
        };
        notifyIcon.BalloonTipIcon = ToolTipIcon.None;
        notifyIcon.BalloonTipText = String.Format("It is nearing {0} would you like to clock out?", ClockOutTime.ToString("HH:mm"));
        notifyIcon.BalloonTipTitle = "Organiser";

        //START REMINDER THREAD
        _Reminder = new Thread(new ThreadStart(_reminder));
        _Reminder.Start();
    }

这是调用气球提示的地方

        private void Remind()
    {
        //IF USER IS IN REMIND USER TO CLOCK OUT
        if (!Global.CurrentUser.In)
        { this.Dispose(); return; }
        notifyIcon.Visible = true;
        notifyIcon.ShowBalloonTip(20000);
        //Thread.Sleep(5000);
        //notifyIcon.Visible = false;
        //this.Dispose();
    }

还尝试使用IntelliSense为您创建的方法,但也没有。

已更改为

            NotifyIcon notifyIcon = new NotifyIcon()
        {
            Icon = Properties.Resources.MainIcon,
            Text = "Organiser",
            BalloonTipIcon = ToolTipIcon.None,
            BalloonTipText = String.Format("It is nearing {0} would you like to clock out?", ClockOutTime.ToString("HH:mm")),
            BalloonTipTitle = "Organiser",
        };

        notifyIcon.BalloonTipClosed += new EventHandler(BalloonClicked);
        notifyIcon.Click += new EventHandler(BalloonClicked);
        notifyIcon.BalloonTipClicked += new EventHandler(BalloonClicked);
        notifyIcon.BalloonTipShown += new EventHandler(BalloonClicked);
        notifyIcon.Disposed += new EventHandler(BalloonClicked);
        notifyIcon.MouseClick += (object sender, MouseEventArgs e) => MessageBox.Show(e.X.ToString());
        notifyIcon.Visible = true;
        notifyIcon.ShowBalloonTip(20000);

现在所有代码都在一个方法中,但它仍然无效。没有事件发生

已更新

这是因为我没有在主线程上运行该方法。谢谢你的帮助

1 个答案:

答案 0 :(得分:-1)

尝试更换以下内容:

notifyIcon.BalloonTipClicked += delegate
    {
        //NOT GOING HERE :(
        MessageBox.Show("TEST");
    };

使用:

notifyIcon.BalloonTipClicked += (sender, e) => {
    //Your code.
};