If your goal is to make sure the NumericUpDown Control is reset to its minimum value if the value-entry-field is empty, and the NumericUpDown Control loses Focus:
private void numericUpDown1_Leave(object sender, EventArgs e)
{
if (numericUpDown1.Text == String.Empty)
{
numericUpDown1.Value = numericUpDown1.Minimum;
numericUpDown1.Text = numericUpDown1.Value.ToString();
}
}
Handling backspace and delete, and detecting when the value-entry field is empty can be done:
private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
{
if (numericUpDown1.Text == String.Empty)
{
numericUpDown1.Value = numericUpDown1.Minimum;
numericUpDown1.Text = numericUpDown1.Value.ToString();
}
}
However, using the second code example creates an "awkward" behavior: when the minimum value is reset, and its Text representation reset, the cursor will insist on going to the start-position in the edit field (no way to move that cursor that I know of in C#). If I want to edit "beyond" first digit of the number, I've got to manually shift the cursor position.
And, assume you set the minimum value to #40, and then you want to edit that and change it to #80; if you are using the second code example, if you clear the value-entry-field, it will reset to #40 again, so you've got to do a little dance with moving the insertion cursor and selecting/or deleting the first digit only.
Depending on the person, given you can use the arrow-keys to move the insertion point, this "price" you pay with code example 2 may be okay, or not okay.
The NumericUpDown Control, like many of the MS supplied Controls, has its quirks, and limits. The requirement ... as you can see in both code examples above ... to have to re-draw the Text in the value-entry-field after we've successfully set the Value in code is rather stupid (and there is no combination of Update/Refresh/Invalidate that I've found that can remove that requirement).