Kalpesh Chhatrala
Kalpesh Chhatrala

Reputation: 139

System Tray Icon Double / Single Click Issue

I created System Tray Icon and added two click Events Double Click, Single Click. i tried mouse click and normal both click.

    SysTray.MouseClick += new MouseEventHandler(SysTray_MouseClick);
    SysTray.MouseDoubleClick += new MouseEventHandler(SysTray_MouseDoubleClick);

    void SysTray_MouseClick(object sender, MouseEventArgs e)
    {
        SingleClick = true;

        if (e.Button == MouseButtons.Left)
        {
            System.Threading.Thread.Sleep(300);
            if (SingleClick)
            {
                //To Do
            }
        }
    }

    void SysTray_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            SingleClick = false;
            System.Threading.Thread.Sleep(20);
            //To Do 
        }
    }

If I Double Click on System Tray Icon it will automatic execute Single Click Icon.

how can i ractify double / single click issue

Upvotes: 3

Views: 2369

Answers (2)

Arjailer
Arjailer

Reputation: 801

I know it's been a while, but I just solved this using a timer:

Timer singleClickTimer = new Timer();

private void trayIcon_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        // Give the double-click a chance to cancel this
        singleClickTimer.Interval = (int) (SystemInformation.DoubleClickTime * 1.1);
        singleClickTimer.Start();
    }
}

private void singleClickTimer_Tick(object sender, EventArgs e)
{
    singleClickTimer.Stop();

    // do single click here
}

private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        // Cancel the single click
        singleClickTimer.Stop();

        // do double click here
    }
}

Upvotes: 5

Poomrokc The 3years
Poomrokc The 3years

Reputation: 1099

Like hans said in comment. Double click always start with singleclick. So I present you one of my solution

In your class , create a boolean.

 bool click=false;

Then for the method.

 void SysTray_MouseClick(object sender, EventArge e)
     {
        click=true;
        System.Threading.Thread.Sleep(300);//Test with this amount!!
        if(click)
          {
            //codes go here
          }
     }
 void Systray_DoubleClick(object sender , EventArge e)
     {
        System.Threading.Thread.Sleep(20);//This too!
        click=false; // If it is doubleclick cancel the single click event.
        //codes go here.
     }

The main concept is if it is doubleclick cancel the single click event. I run a stopwatch and doubleclick should take 200-350 ms. So adjust the timeing you want. But on the other hand. More timing = the slower your code process

Edit:This logic burst my head!! I end up with this code , try it.

Upvotes: 0

Related Questions