简体   繁体   中英

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;
        }
    }
}

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://docs.microsoft.com/en-us/dotnet/desktop/winforms/input-mouse/how-to-distinguish-between-clicks-and-double-clicks?view=netdesktop-6.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM