简体   繁体   中英

C#, Windows Forms: NotifyIcon with ShowBalloonTip in Click event no longer fires the DoubleClick event

When using a NotifyIcon in Windows Forms/C#/.Net Framework 2.0, if I display a Balloon Tip Text in the MouseClick or Click events, none of the DoubleClick or MouseDoubleClick events will fire:

    private void notifyIcon_DoubleClick(object sender, EventArgs e)
    {
        MessageBox.Show("double click"); // this is never called on double-click
    }

    private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show("mouse double click"); // this is never called on double-click
    }

    private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.notifyIcon.BalloonTipText = "Some Info";
            this.notifyIcon.ShowBalloonTip(1000);
        }
    }

If I double-click the notify icon, I get the Balloon Tip showed/refresh twice, but no message box.

I'm using Visual Studio 2010 and Windows 7 Ultimate 64-bit.

Thanks in advance for any help!

You need to check if it's a double click in you notifyIcon_MouseClick

 private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) 
        {
          if (e.Clicks < 2) //not a doubleclick
          {
              this.notifyIcon.BalloonTipText = "Some Info";
              this.notifyIcon.ShowBalloonTip(1000);
          }
        }
    }

This is probably a bug. I don't know who is to blame.

e.Clicks is zeroed upon calling notifyIcon.ShowBalloonTip. If the showballoontip is given an invalid argument, an exception is written to the console and e.Clicks finally becomes higher than 1.

I am using a logitech g5 mouse with firmware 1.2. It is not the logitech setpoint software. Rolled back to the microsoft driver as well, didn't help.

I am running on .NET framework 4.0, windows 7 64 bit and Visual Studio 2010.

It is a really weird bug. Sometimes exactly the same build does sometimes not show the bug. In the same session it can occur again. After a rebuild, the bug is present again.

This is a bug indeed. I found a better workaround than any suggested. Do not attempt to launch the BalloonTip from within the Click or DoubleClick events at all. Instead, you can create a boolean flag globally, and then set the flag in the Click event. Also create a Timer() that fires periodically, every 1000ms was fine for me. When the timer fires, it checks the flag, and if it is set, then fires ShowBalloonTip() accordingly, resetting the flag also. This doesn't break the Click or DoubleClick behaviors and still achieves the desired behavior for the BalloonTip, from changes occurring within Click or DoubleClick events on the NotifyIcon. Worked great for me! Sorry I don't have code snipet easily to provide but this description should give the idea of how to do it easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM