How to make Numericupdown return to the minimum value when Upbutton is clicked and the current value is the maximum?

Asked

Viewed 74 times

1

I’m wearing a NumericUpDown in an application where the user can set a desired time for any action to be performed. There are three NumericUpDown: one for the hour (from 0 to 23), another for minute (0 to 59) and another to second (0 to 59).

The problem is that when the user increments the values by means of the NumericUpDown when the current value is the maximum value set (23 for hours and 59 for minutes and seconds), of course the value is no longer incremented. However, I would like to know if there is any way to make the values return to the minimum in this situation (i.e., to 0).

1 answer

1


One way to solve would be to compare the current value with the minimum and maximum allowed value.

When the value reaches the minimum, change the value to the maximum and when it reaches the maximum, change the value to the minimum.

At the event Click of NumericUpDown do:

private void numericUpDown2_Click(object sender, EventArgs e)
{
    decimal valor = numericUpDown2.Value;

    if (valor.Equals(numericUpDown2.Minimum)) {
        numericUpDown2.Value = numericUpDown2.Maximum;
    } 

    if (valor.Equals(numericUpDown2.Maximum)) {
        numericUpDown2.Value = numericUpDown2.Minimum;
    }  
}
  • Thank you, @stderr !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.