I have several controls of type numericUpDown, the fact is that each of them has a minimum value and another maximum value by default. The user has a keypad where he presses a button with a number from 1 to 9 and a special button to erase the last digit. By clicking on a button that is not the delete, the value of the numeric is increased and is what can trigger the exception, just as if the delete we are with a value below the minimum What I need is that when the user puts a value higher than the maximum, capture the exception and put the maximum value by default of that control, and the same for the minimum value.
I'm trying to do it using both OverflowException (which does not fit) as Exception, for which obviously it enters but where I do not see in any place (except the message) the value that the numericUpDown has when the exception has been skipped.
All the buttons implement the code that I will put below and the last number is the last number on which the focus was kept so that it is the one that is modified. (Button that ends in B is the one that deletes the last digit)
Basically what I need is to know that Excepcion must be sought to collect that value.
private void clickBoton(object o, EventArgs e)
{
string aux = ((Button)o).Name.Substring(3, 1);
string tag = (((Button)o).Tag == null) ? "" : ((Button)o).Tag.ToString();
try
{
if (aux == "B")
{
string auxB = decimal.ToInt32(ultimoNumerico.Value).ToString();
if (aux.Length > 1)
{
auxB = auxB.Substring(0, auxB.Length - 1);
ultimoNumerico.Value = int.Parse(auxB);
}
else
{
ultimoNumerico.Value = ultimoNumerico.Minimum;
}
}
else
{
Int32 auxNum = decimal.ToInt32(ultimoNumerico.Value) * 10 + int.Parse(aux);
ultimoNumerico.Value = auxNum;
}
}
catch (OverflowException ox)
{
object ob = ox.Data;
}
catch (Exception ex)
{
if (ultimoNumerico.Value > ultimoNumerico.Maximum)
{
ultimoNumerico.Value = ultimoNumerico.Maximum;
}
else
{
ultimoNumerico.Value = ultimoNumerico.Minimum;
}
}
}