Acceleration of NemericUpDown

Mar 16, 2014 at 3:23 PM
Per my tests, the following method is never called:
        private void OnDecrementButtonIsPressedChanged(object decrementButton, bool isPressed)
        {
            // TODO: The thinking was to handle speed and acceleration of value changes manually on a regular Button when it is pressed.
            // Currently just using RepeatButtons
        }
To accelerate the the value change, I have added/modified the following for my own use (showing the part for the InrementButton. Similar code for the DecrementButton is omitted) :
protected override void OnApplyTemplate()
{
     ...
    if (_incrementButton != null)
    {
        ...
        _incrementButton.Holding += OnIncrementButtonHoldingChanged;
       ...
    }
}
bool bHolding = false;
void OnIncrementButtonHoldingChanged(object sender, HoldingRoutedEventArgs e)
{
    switch(e.HoldingState)
    {
    case Windows.UI.Input.HoldingState.Started:
        bHolding = true;
        break;
    case Windows.UI.Input.HoldingState.Completed:
    case Windows.UI.Input.HoldingState.Canceled:
    default:
        bHolding = false;
        break;
    }
}
private bool Increment()
{
    if(bHolding)
    {
        return SetValueAndUpdateValidDirections(this.Value + this.LargeChange);
    }
    else
    {
        return SetValueAndUpdateValidDirections(this.Value + this.SmallChange);
    }
}
I am wondering if this can be adopted. I hate to branch out my own.

Hong
Coordinator
Mar 17, 2014 at 6:42 PM
Yes, I had some plans to implement it inspired by Windows Forms' NumericUpDown, but these never materialized. If I have a moment I'll review your suggestion and might include it. Meanwhile I recommend that you grab the source for NumericUpDown from the toolkit and include the modified version in your code so you are not dependent on my free time and priorities to update the control.
Mar 17, 2014 at 7:14 PM
Thank you for the consideration.

Yes, I have finally started using the source code in lieu of the package dll. The app using the code has just been published by Microsoft.

I am all set for now, but I am concerned about the future. When you have a new version, I will download and use it. If the change is not adopted, I will have to keep in mind to modify the code again.

I have found a problem with the previously described approach - it does not work for non-touch device. I have the feeling the majority of Win8 machines are non-touch devices. Here is the new code that has been tested on both touch and non-touch screens. It works quite well. Thank you for the control.
bool bHolding = false;
DateTime dtLastClick = DateTime.Now;
private void OnIncrementButtonClick(object sender, RoutedEventArgs routedEventArgs)
{
    CheckHolding();
    this.Increment();
}

private void CheckHolding()
{
    //Tests show that the interval of repeated clicks is about 120 ms
    if ((DateTime.Now - dtLastClick).TotalMilliseconds < 150)
    {
        bHolding = true;
    }
    else
    {
        bHolding = false;
    }
    Debug.WriteLine("Time elapse (ms):" + (DateTime.Now - dtLastClick).TotalMilliseconds);
    dtLastClick = DateTime.Now;
}

private bool Increment()
{
    if (bHolding)
    {
        return SetValueAndUpdateValidDirections(this.Value + this.LargeChange);
    }
    else
    {
        return SetValueAndUpdateValidDirections(this.Value + this.SmallChange);
    }
}
Coordinator
Mar 17, 2014 at 7:40 PM
On the other hand if I make a change that conflicts with yours - you might not want to use mine. Good luck!
What's the name of your app by the way?
Mar 17, 2014 at 7:53 PM
If you implement the acceleration in a different way, I will use it for sure. I do not have to use my way. I will email you the app name.