Search code examples
c#eventsdelegatesnotifyicon

NotifyIcon BalloonTipClicked Event not Firing


Hi I'm trying to run some code when a user clicks on a balloon tip. At the moment it's showing the balloon tip but when I click on it the delegate is not being fired.

This is where the NotifyIcon is created

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

This is where the balloon tip is being called

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

Also tried it with the method that IntelliSense creates for you but that didn't work either.


Changed to

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

Now all of the code is in one method but it's still not working. None of the events fire


UPDATED

This was due to me not running the method off the main thread. Thanks for your help


Solution

  • Try replacing the following:

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

    with:

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