Keys Angelo
Keys Angelo

Reputation: 3

Perform Action When Button Is Held And Another Action When Button Is Released

I am using visual studios to create a Windows IoT app. On the GUI of the app there is a button that when held down with a mouse click or a key being pressed it performs an action, in this case a text box changes its text and a motor is run. When the button is released the text box changes its text and the motor is stopped.

I created a button in the .xml file and button_holding event in the .cs, but this doesn't perform any of the actions.

Upvotes: 0

Views: 213

Answers (2)

Rita Han
Rita Han

Reputation: 9700

On the GUI of the app there is a button that when held down with a mouse click or a key being pressed it performs an action, in this case a text box changes its text and a motor is run. When the button is released the text box changes its text and the motor is stopped.

You can use PointerPressedEvent and PointerReleasedEvent to achieve it. This is the code works for me:

public MainPage()
{
    this.InitializeComponent();
    myButton.AddHandler(PointerPressedEvent, new PointerEventHandler(myButton_PointerPressed), true);
    myButton.AddHandler(PointerReleasedEvent, new PointerEventHandler(myButton_PointerReleased), true);
}

private void myButton_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    myText.Text = "Running...";
}

private void myButton_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    myText.Text = "Stopped";
}

Upvotes: 1

WindyHen
WindyHen

Reputation: 328

You can use Holding event to get similar MouseUp and MouseDown in traditional WinForms, like so

private void Button_Holding(object sender, HoldingRoutedEventArgs e)
        {
            if (e.HoldingState == Windows.UI.Input.HoldingState.Started)
            {
                //do things when button on hold
            }
            else if (e.HoldingState == Windows.UI.Input.HoldingState.Completed ||
                e.HoldingState == Windows.UI.Input.HoldingState.Canceled)
            {
                //do things when button release
            }
        }

Upvotes: 0

Related Questions