This is a migrated thread and some comments may be shown as answers.

Long press on a button

2 Answers 2755 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Datafyer
Top achievements
Rank 1
Veteran
Datafyer asked on 12 Feb 2021, 11:59 PM

I have a few RadButton controls where I would like a different command to execute if the user "long presses" on the button.

For instance a single simple click would do 1 thing and a long click and hold then release would do something else.

How would I do that?

2 Answers, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 17 Feb 2021, 09:35 AM

Hello,

To achieve your scenario you can use the PreviewMouseDown and PreviewMouseUp events of the RadButton. In the PreviewMouseDown event handler you can start a timer. In the PreviewMouseUp you can check how long the timer have run and if it is more than a second you can execute your long press logic.

public partial class MainWindow : Window
{
    DispatcherTimer timer;
    double elapsedTime = 0;

    public MainWindow()
    {
        InitializeComponent();
        timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0,0,0,0,100);
        timer.Tick += Timer_Tick;
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        elapsedTime += 1;
    }

    private void RadButton_MouseDown(object sender, MouseButtonEventArgs e)
    {
        timer.Start();
    }

    private void RadButton_MouseUp(object sender, MouseButtonEventArgs e)
    {
        timer.Stop();
        if(elapsedTime >1)
        {
            // execute long press logic
        }
         elapsedTime = 0;
    }
}

You can modify the timer so it fit in your application.

Regards,
Dinko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Datafyer
Top achievements
Rank 1
Veteran
answered on 18 Feb 2021, 06:13 PM
Thanks,
Tags
Buttons
Asked by
Datafyer
Top achievements
Rank 1
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Datafyer
Top achievements
Rank 1
Veteran
Share this question
or