Search code examples
c#winformsnumericupdown

Modify the behavior of NumericUpDown Control as NumericCyclic Control in winform application


I am intended to achieve following behavior in the NumericUpDown Control used in the winform application.

When we click the down arrow key of the NumericUpDown control the value of the control decreases and in subsequent clicks its value continues to decreases until it reaches minimum value. After that the value remain unchanged for further clicking in down arrow key. The opposite behavior also true for clicking the up arrow key of the NumericUpDown control i.e. after reaching maximum value the value remain unchanged for further click in up arrow of the control.

I am trying achieve following behavior:

  1. After reaching maximum value,if user clicks up arrow key the value set to minimum value of the control.

  2. After reaching minimum value,if user clicks down arrow key the value set to maximum value of the control.


Solution

  • This is not a good behavior for NumericUpDown control, but if you want so, you can Inherit from NumericUpDown and override UpButton() and DownButton() like this

        public override void UpButton()
        {
            if (Value == Maximum)
                Value = Minimum;
            else
                base.UpButton();
        }
    
        public override void DownButton()
        {
            if (Value == Minimum)
                Value = Maximum;
            else
                base.DownButton();
        }
    

    and use that inherited control instead of NumericUpDown.