Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading