Consider the following code: I have a text block that is an on / off button in WPF. This is just text inside an ellipse that says ON / OFF. When the user presses the button and holds the left mouse button for ONE second, he will execute the code βturn on the deviceβ if the device is not already turned on. If the user holds the ON / OFF button for three seconds or more (holds the left mouse button HELD down), the device will turn off.
A few problems that I miss the boat with. 1. A tick event does not fire when the mouse button is held, even though the timer is running. 2. The do / while loop never exits despite raising the button
Thanks!
public int TimerCount = 0;
private void ButtonPressTimer(object sender, EventArgs e)
{
TimerCount = TimerCount + 1;
}
private void txtBlockOnOff_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var buttonPressTimer = new DispatcherTimer(new TimeSpan(0, 0, 0, 1), DispatcherPriority.Normal, ButtonPressTimer, this.Dispatcher);
do
{
if (!buttonPressTimer.IsEnabled)
buttonPressTimer.Start();
}
while (e.ButtonState == MouseButtonState.Pressed);
buttonPressTimer.Stop();
if (TimerCount >= 3)
{
}
else
{
}
}