C#C
C#7mo ago
FiftyTifty

Ensure there is always a value in NumericUpDown?

This has been a pain and I'm at a loss. With NumericUpDown forms, it is possible to make the form have no content by pressing backspace or whatever weird way a user has come up with.

I can't see a way to make sure the form always has a numerical value? I tried adding this function as an event handler for OnLeave, OnEnter, and ValueChanged:

C#
public void EnsureNumericHasNumber (object sender, EventArgs e)
{

    NumericUpDown numCurrent = (NumericUpDown)sender;

    if (numCurrent.Text == "")
    {
        numCurrent.Value = 0;
    }

}
...

numCurrent.Enter += EnsureNumericHasNumber;
numCurrent.Leave += EnsureNumericHasNumber;
numCurrent.ValueChanged += EnsureNumericHasNumber;
image.png
Was this page helpful?