Question

I'm hoping someone has a solution to this problem. I have a series of NumericUpDown controls which are used in a calculation to find the total number of minutes elapsed for a number of trips where the hours and minutes are known for each trip. The problem I am having, is that when a user manually clears one of the NUD controls by entering the NUD and then using the backspace key to clear out the text, the value is still retained and causes unexpected results in the calculation.

Here is an example of what I am talking about: enter image description here Above calulates 90 minutes

enter image description here Above calculates 120 minutes

enter image description here After the trip2 minutes text is cleared by backspacing, still calculates 120 minutes

Is there any way to change this behavior? Maybe I am missing something obvious. Any ideas?

Thanks in advance!

Was it helpful?

Solution

You can try using the leave event of the control to check the "Text" property:

numericUpDown1.Leave += numLeave;
numericUpDown2.Leave += numLeave;

void numLeave(object sender, EventArgs e) {
  NumericUpDown nud = sender as NumericUpDown;
  if (nud != null) {
    if (nud.Text == string.Empty) {
      nud.Value = nud.Minimum;
      nud.Text = nud.Value.ToString();
    }
  }
}

Note that the Text property is hidden from Intellisense, but it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top