Search code examples
c#.netwinformsnumericupdown

How to swap arrow buttons functionality on NumericUpDown control?


I want to swap the Up and Down arrows (functionality only, not the actual arrows) on NumericUpDown control. For example on clicking the UP arrow the value should decrease and similarly on clicking the Down arrow the value should increase.

I tried to inherit from NumericUpDown class and override UpButton() and DownButton(). I thought that simply swapping the code in between these two methods should work. But upon pasting the DownButton() code into overridden UpButton(),

public class MyCustomNumericUpDown : NumericUpDown
{
    public override void UpButton()
    {
        SetNextAcceleration();
        if (base.UserEdit)
        {
            ParseEditText();
        }

        decimal num = currentValue;
        try
        {
            num -= Increment;
            if (num < minimum)
            {
                num = minimum;
                if (Spinning)
                {
                    StopAcceleration();
                }
            }
        }
        catch (OverflowException)
        {
            num = minimum;
        }

        Value = num;
    }
}

the above program throws the following errors.

The name 'SetNextAcceleration' does not exist in the current context
The name 'currentValue' does not exist in the current context
The name 'minimum' does not exist in the current context
The name 'Spinning' does not exist in the current context
The name 'StopAcceleration' does not exist in the current context
The name 'minimum' does not exist in the current context

I see all these methods and variables are set to private in the base class. Which might be throwing these "does not exist" errors.

Anyone know how to do this? I just want the arrow buttons functionality swapped. For example on clicking the UP arrow the value should decrease and similarly on clicking the Down arrow the value should increase.


Solution

  • You can derive from the NumericUpDown and override UpButton and DownButton methods like this (by calling base.TheOtherMethod to swap the functionality):

    public class CrazyNumericUpDown : NumericUpDown
    {
        public override void UpButton()
        {
            base.DownButton();
        }
        public override void DownButton()
        {
            base.UpButton();
        }
    }