开发者

Implement missing MouseDoubleClick using MouseClick Winforms control event

开发者 https://www.devze.com 2023-04-12 23:59 出处:网络
I have a control that has MouseClick event listener implemented, while MouseDoubleClick listener is 开发者_StackOverflow社区missing.

I have a control that has MouseClick event listener implemented, while MouseDoubleClick listener is 开发者_StackOverflow社区missing.

I've already managed to distinguish between click and dblclick events in javascript using my custom version of jQuery.DoubleTap plugin: bit.ly/roCV7h

So I would like to use a similar trick in C#: to set a delay on the click event execution and cancel it if fires again before the time out.

I found a lot of solutions on stackoverflow: but all of them are not thread-safe or put a main (GUI) thread to sleep.


I would use a timer and store the number of clicks. When the timer "ticks" then check the click count and fire the relevant event.

pseudo code (sorry don't recall timer functions of top of head)...

Timer timer = new Timer(); //maybe 500 ms elapsed period
int mouseCount = 0;

void OnMouseClick()
{
   if(!timer.Enabled)
      //start timer
   mouseCount++;
}

void OnTimerTick()
{
   timer.Stop();
   if(mouseCount == 1)
      //fire single mouse click event
   else if(mouseCount > 1)
      //fire double mouse click event
   mouseCount = 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消