Click here to Skip to main content
12,692,105 members (26,074 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
I am creating an application in C# in visual Studio 2008 where in a single button will have 2 uses and it will depend on whether the user just presses the button or long presses the button.

i thought of adding a timer but it is a very clumsy way and i was thinking if there was an inbuilt way for this
Posted 20-Nov-12 6:47am
Sergey Alexandrovich Kryukov 20-Nov-12 12:16pm
I answered, but in you need further detail, you need to tag you UI library you use.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Easy. Except for button press, there are lower-level events, button down and then button up. You can detect the time between these events. Further detail depends on UI library you want to use and application type, but the code would be very similar.


In most applications, I would not recommend you to operate on timing between down and up events, as the users are not used to it, this is not a common UI style. But sometimes we want to go out of common UI styles, then your idea might work for you. But even then, don't assume the user will get it if you don't care about explaining this unusual feature somehow.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can add event mouseLeftButtonDown to this button. And detect the long click event using this method

        /// <summary>
        /// Detect Mouse Long Click
        /// </summary>
        /// <param name="element">Framework</param>
        /// <param name="duration">Set duration of long click</param>
        /// <returns>true: Long Click false: Not Long Click</returns>
        public static Task<bool> MouseDown(this FrameworkElement element, TimeSpan duration)
            DispatcherTimer timer = new DispatcherTimer();
            TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
            timer.Interval = duration;

            MouseButtonEventHandler touchUpHandler = delegate
                if (task.Task.Status == TaskStatus.Running)

            element.PreviewMouseUp += touchUpHandler;

            timer.Tick += delegate
                element.PreviewMouseUp -= touchUpHandler;

            return task.Task;

Call the method in your event btn_MouseLeftButtonDown
For example

private async void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        boolean isLongPress = await MouseDown(e.Source as Button,           TimeSpan.FromSeconds(0.4));
       if (isLongPress) {
          // long press
       } else {
        //short press

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 8 Mar 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100