2

I have a NumericUpDown control with increment of 5. Entering 1 using the keyboard is possible and shall always be possible.
But when I click the Up-button I want not to have a 6 but a 5, always 5, 10, 15...

How is this possible? In ValueChange this is not possible, because I want the possibility to enter every single number by keyboard.

4
  • This may help: learn.microsoft.com/es-es/dotnet/api/… Commented Jul 31, 2020 at 11:44
  • When you define the increment of 5 it's considered as a role to your NumericUpDown control. You shouldn't allow to input any value because it breaks your previous restriction of increment 5. Commented Jul 31, 2020 at 11:47
  • But how to detect if it came from the buttons and not from the textfield? Commented Jul 31, 2020 at 11:47
  • 2
    You can create custom user control with textbox and two buttons and handle it as you like. As a bonus you can add more buttons (+5, +10, -5, -10, etc.) to enrich user experience. Commented Jul 31, 2020 at 12:00

2 Answers 2

3

You can derive a Custom Control from NumericUpDown, override both UpButton() and DownButton() to provide a custom logic to apply to the Increment value.

Here, setting the Increment to 5, the NumericUpDown Buttons (and the UP and DOWN keys, if InterceptArrowKeys is set to true), will increment or decrement the Value by 5.
If the User enters a different value with the keyboard, the value is accepted, unless it exceeds the Maximum and Minimum pre-sets. In this case, the Control is reset to the Maximum or Minimum value.

E.g., a User enters 16: if the UpButton is pressed, the value will be set to 20, to 15 if the DownButton is pressed instead.
In either case, the Value entered cannot exceed the upper and lower bounds.

Note that neither base.UpButton() or base.DownButton() are called, since these would adjust the Value to the Increment set using the standard method.

class NumericUpDpwnCustomIncrement : NumericUpDown
{
    public NumericUpDpwnCustomIncrement() { }

    public override void UpButton() => 
        Value = Math.Min(Value + (Increment - (Value % Increment)), Maximum);

    public override void DownButton() => 
        Value = Math.Max(Value - (Value % Increment == 0 ? Increment : Value % Increment), Minimum);

    protected override void OnValueChanged(EventArgs e)
    {
        base.OnValueChanged(e);
        Value = Math.Max(Math.Min(Value, Maximum), Minimum);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use NumbericUpDown Click event. It is called when up/down buttons are clicked.

private void OnClick(object sender, EventArgs e)
{
    numericUpDown.Value -= numericUpDown.Value % 5;
}

Edit: since the event also gets called when the Textfield is clicked, I propose another solution.

You can override NumericUpDown methods UpButton and DownButton to trigger an event.

public class MyNumericUpDown : System.Windows.Forms.NumericUpDown
{
    public event Action OnButtonPressed;
    
    public override void UpButton()
    {
        base.UpButton();
        OnButtonPressed?.Invoke();
    }

    public override void DownButton()
    {
        base.DownButton();
        OnButtonPressed?.Invoke();
    }
}

And inside YourForm.Designer.cs rewrite this.numericUpDown = new System.Windows.Forms.NumericUpDown(); to this.numericUpDown = new YourNamespace.MyNumericUpDown();.

Then in your Form construcor add:

numericUpDown.OnButtonPressed += () => numericUpDown.Value -= numericUpDown.Value % 5;

1 Comment

This also is called when I click into the Textfield to enter a number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.