Search code examples
c#mousedownclick-counting

C# MouseButtonEventArgs ClickCount - How do I separate what happens in one or two clicks?


My goal is that if you click once on a TextBlock, it copies the text and if you click twice on it, it executes something else without copying the text.

I thought a switch would be handy, but it still copies the text when double-clicked.

private void Path_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        switch (e.ClickCount)
        {
            case 1:
                System.Windows.Clipboard.SetText(ViewModel.MainPath);
                break;
            case 2:
                StartProcess.Start(ViewModel.MainPath, true);
                break;
        }
    }
}

Solution

  • Your method is executing after the first click, you can try to put a delay on it so it will be able to count the second click, better to save each click on an state variable.

    Or look it up here - https://learn.microsoft.com/en-us/dotnet/desktop/winforms/input-mouse/how-to-distinguish-between-clicks-and-double-clicks?view=netdesktop-6.0