0

I have a NotifyIcon and I set balloon text with MouseMove event. The balloon text comes from a database. This results continuous database query.

private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
{
    //database operations.......
}

How can I prevent this? I want to set balloon text once when mouse on NotifyIcon.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
kovak
  • 73
  • 1
  • 9

2 Answers2

1

Use the BalloonTipShown event (https://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.balloontipshown(v=vs.110).aspx) The behaviour you are looking for matched that event alot better then the MouseMove event

Kevin Sijbers
  • 814
  • 7
  • 19
  • Also it's good to cache the results and not go to the database for the same record again and again on every tooltip. – Evk May 17 '17 at 11:50
  • @Kevin Sijbers It does not seem to work, nothing happen. – kovak May 17 '17 at 11:55
  • Are you using System.Windows.Forms.NotifyIcon, not some other class? How is the event assigned to the object? This should load database information once when displaying the tooltip – Kevin Sijbers May 17 '17 at 12:00
  • @Kevin Sijbers Yes, i am using the standard NotifyIcon. `private void notifyIcon1_BalloonTipShown(object sender, EventArgs e) { notifyIcon1.Text = "bbbbbb"; }` NotifyIcon text remains unchanged. – kovak May 17 '17 at 12:33
0

Another approach would be to add a Timer to your Form and set its Interval to a delay like 1 second. This delay would be how often the user could hit the database. Setup a Flag that gets reset by the Timer and check it in your NotifyIcon event. Something like:

    private bool AllowUpdate = true;

    private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
    {
        if (AllowUpdate)
        {
            AllowUpdate = false; // don't allow updates until after delay

            // ... hit the database ...
            // ... update your text ...

            timer1.Start(); // don't allow updates until after delay
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // reset so it can be updated again
        AllowUpdate = true;
        timer1.Stop();
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40