Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 28-May-18 2:11am
Comments
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.
--SA

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.

[EDIT]

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.

—SA
 
Share this answer
 
v3
You can add event mouseLeftButtonDown to this button. And detect the long click event using this method

C#
/// <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
    {
        timer.Stop();
        if (task.Task.Status == TaskStatus.Running)
        {
            task.SetResult(false);
        }
    };

    element.PreviewMouseUp += touchUpHandler;

    timer.Tick += delegate
    {
        element.PreviewMouseUp -= touchUpHandler;
        timer.Stop();
        task.SetResult(true);
    };

    timer.Start();
    return task.Task;
}


Call the method in your event btn_MouseLeftButtonDown
For example

C#
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
       }
    }
 
Share this answer
 
v3
my shortest solution for this problem:

C#
private bool mouseUp = false;
private const int holdButtonDuration = 2000; //milliseconds
private void btnTest_MouseDown(object sender, MouseEventArgs e)
{
    mouseUp = false;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    while (e.Button == MouseButtons.Left && e.Clicks == 1 && (mouseUp == false && sw.ElapsedMilliseconds < holdButtonDuration))
        Application.DoEvents();
    if (sw.ElapsedMilliseconds < holdButtonDuration)
        btnTest_ShortClick(sender, e);
    else
        btnTest_LongClick(sender, e);
}
private void btnTest_MouseUp(object sender, MouseEventArgs e)
{
    mouseUp = true;
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900