Collect the exception value in numericUpDown

1

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;
            }
        }
    }
    
asked by U. Busto 03.01.2018 в 13:05
source

2 answers

1

The concrete answer to your question is that it is not possible to collect the value of the exception that throws the control NumericUpDown , unless you are willing to make a parse of the message itself, that does not seem like a good idea.

The reason is obvious when we examine the source code for the Value property of the control NumericUpDown ( source code ):

public Decimal Value {
    get {
        if (UserEdit) {
            ValidateEditText();
        }
        return currentValue;
    }

    set {
        if (value != currentValue) {

            if (!initializing && ((value < minimum) || (value > maximum))) {
                throw new ArgumentOutOfRangeException("Value", SR.GetString(SR.InvalidBoundArgument, "Value", value.ToString(CultureInfo.CurrentCulture), "'Minimum'", "'Maximum'"));
            }
            else {
                currentValue = value;                       

                OnValueChanged(EventArgs.Empty);
                currentValueChanged = true;    
                UpdateEditText();
            }
        }
    }
}

In particular, note how the control creates the exception ArgumentOutOfRangeException using the constructor with 2 parameters only ( string paramName, string message ). This is why the property ActualValue remains at null .

Ideally, the control would create and throw the exception in this way, using the constructor with 3 parameters ( string paramName, object actualValue, string message ):

throw new ArgumentOutOfRangeException(
    "Value",
    value, // esto es lo que hace falta
    SR.GetString(SR.InvalidBoundArgument, "Value", value.ToString(CultureInfo.CurrentCulture), "'Minimum'", "'Maximum'"));

If it had been this way, then the ActualValue property would contain the desired value when catching the exception. But since we have no control over the control code NumericUpDown , you have no choice but to use a logic that does not depend on this exception.

    
answered by 03.01.2018 / 15:05
source
0

In this case, try saving to a variable that you have reached from the catch the value you want to write:

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();
        var valor = ultimoNumerico.Minimum;

        if (aux == "B")
        {
            string auxB = decimal.ToInt32(ultimoNumerico.Value).ToString();
            if (aux.Length > 1)
            {
                auxB = auxB.Substring(0, auxB.Length - 1);
                valor = int.Parse(auxB);
            }
            else
            {
                valor = ultimoNumerico.Minimum;
            }
        }
        else
        {
            Int32 auxNum = decimal.ToInt32(ultimoNumerico.Value) * 10 + int.Parse(aux);
            valor = auxNum;
        }
        try
        {
            ultimoNumerico.Value = valor;
        }

        catch (ArgumentOutOfRangeException ex)
        {
            if (valor > ultimoNumerico.Maximum)
            {
                ultimoNumerico.Value = ultimoNumerico.Maximum;
            }
            else
            {
                ultimoNumerico.Value = ultimoNumerico.Minimum;
            }
        }
    }
    
answered by 03.01.2018 в 13:33