0

I have to execute some code at left click of mouse and I have to perform separate actions on double click but my Canvas doesn't support doubleclick event, so, I am trying to get the work done by clickcount but it also, enters left click event before coming to clickcount = 2 . How do I prevent this? I know I have to use e.handled = true but i can't understand where. Any help would be appreciated.

void Canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    if(e.ClickCount == 2)
    {
     // Some code
    }
    else
    {
        if(e.ChangedButton == MouseButton.Left)
        {
            // some code
        }
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Possible duplicate of [How can I catch both single-click and double-click events on WPF FrameworkElement?](https://stackoverflow.com/questions/2086213/how-can-i-catch-both-single-click-and-double-click-events-on-wpf-frameworkelemen) – IDarkCoder Jul 09 '19 at 06:06

1 Answers1

0

Like @IDarkCoder said, this seems to have already been answered on a different question. The back code should be similar to this:

private Timer _delayTimer;

public Constructor()
{
...
  _delayTimer = new Timer(300);
  _delayTimer.Elapsed += DelayTimer_Elapsed;
}

private void Canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
  if (e.ChangedButton == MouseButton.Left && e.ClickCount == 1)
  {
    _delayTimer.Start();
  }
  else if (e.ClickCount == 2)
  {
    e.Handled = true;
    _delayTimer.Stop();
  }
  else
  {
     _delayTimer.Stop();
  }
}

private void DelayTimer_Elapsed(object sender, ElapsedEventArgs e)
{
 // Do your thing here for Single Left Click.
}

And also make sure that your Canvas has a Background set in the UI, even if it is a Transparent one, or else you will not be able to catch the mouse events.

IDarkCoder
  • 709
  • 7
  • 18
Lupu Silviu
  • 1,145
  • 11
  • 23