Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System Tray notifyIcon won't accept left-click event

I'm creating a System-Tray only application. It's somewhat complicated to have the icon without a main form, but through previous topics on StackOverflow I've worked it out. The right-click works fine, I've linked in a context menu, etc.

I'm having problems with the left-click. As far as I can tell, the "notifyIcon1_Click" event isn't firing at all.

    private void notifyIcon1_Click(object sender, EventArgs e)
    {
        Debug.WriteLine("Does it work here?");

        if (e.Equals(MouseButtons.Left))
        {
            Debug.WriteLine("It worked!");
        }
    }

Neither of those debug lines are outputting, breakpoints in that event don't stop the program, etc.

Am I doing this incorrectly? What should my next step be? I'm coding this in C#, using Windows 7 if that matters at all for taskbar behavior.

like image 692
ck_ Avatar asked Jan 23 '23 18:01

ck_


1 Answers

If you want to determine if it's a left or right click, wire up the MouseClick, rather than click.

That way you get a signature like this:

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
        //Do the awesome left clickness
    else if (e.Button == MouseButtons.Right)
        //Do the wickedy right clickness
    else
        //Some other button from the enum :)
}
like image 187
Tristan Warner-Smith Avatar answered Jan 29 '23 10:01

Tristan Warner-Smith