I have several numericUpDown type controls, 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 delete the last digit. When clicking on a button other than the delete button, the value of the numeric is increased and that is what can trigger the exception, the same as if when we delete we are left with a value below the minimum What I need is that when the user put a value greater than the maximum, catch the exception and put the maximum value by default of that control, and the same for the minimum value.
I am trying to do it using both OverflowException (for which it does not enter) and Exception, for which it obviously enters but where I do not see anywhere (except the message) the value that the numericUpDown has when the exception has been thrown.
All the buttons implement the code that I will put below and lastNumerical is the last numeric on which the focus was held 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 what Exception to look for 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;
}
}
}
The concrete answer to your question is that it is not possible to collect the value of the exception that the control throws
NumericUpDown
, unless you are willing to parse the message itself, which does not seem like a good idea.The reason becomes obvious when we examine the source code for the
Value
control propertyNumericUpDown
( source code ):In particular, notice how the control creates the exception
ArgumentOutOfRangeException
using the constructor with only 2 parameters (string paramName, string message
). It is because of this that the propertyActualValue
remains anull
.Ideally, the control would have created and thrown the exception like this, using the 3-parameter constructor (
string paramName, object actualValue, string message
):If it had been this way, then the property
ActualValue
would contain the desired value when catching the exception. But since we don't have control over the control's codeNumericUpDown
, you have no choice but to use logic that doesn't depend on this exception.In that case, try saving the value you want to write in a variable that you have scope from the catch: