-2

I have two IntegerUpDown Controls, and I need to compare in the MouseLeave_Event , I used int.Parse but when I tried this code I got an exception:

   if (int.Parse(minUpdown.Text.ToString()) < 
   int.Parse(maxUpdown.Text.ToString()))
                    {
                      // do something
                    }

Exception:

Input string was not in a correct format

I have searched in Stackoverflow for a solution to avoid this exception, I saw that the best solution is using int.TryParse, but I don't know how can I use it in comparison instruction that returns boolean.

The scenario that should be applied is like this:

  • I have already an IntegerUpdown_MouseLeave_Event and the code that I have posted is mentioned in the event, so the integerUpdown controls could be empties or can have a value, so if the user tries to enter a character ( not a number ) for example a in the IntegerUpDown and this IntegerUpDown have an old value for example 3, so when he moves the mouse outside the control, the IntegerUpDown should take 3 instead of the character entered by the user without showing any message or any exception.

Note: I wouldn't like to use minUpdown.Value in this case for some reason because the value that I got when the event is fired is the old value, not the value in real-time.

  • So you are comparing `resultmin < resultmax`, but what if one of them fails to parse? Do you want the comparison to return true, or false, or do something completely different? – Sweeper Apr 15 '21 at 11:36
  • Yes I would like to compare `resultmin` and `resultmax` , if one fails, it should return the default value ( previous value ) and remove the character entered by the user , that's what I need –  Apr 15 '21 at 11:38
  • Why don't you use the `Value` property that returns an `int?`? And you don't need to reset the previous value in your event handler. The control does this for you, doesn't it? – mm8 Apr 15 '21 at 13:56

1 Answers1

0

The answer for the first question 'How to use TryParse and compare the values' looks like this:

int.TryParse(minUpdown.Text?.ToString(), out var min);
int.TryParse(maxUpdown.Text?.ToString(), out var max);

if (min < max)
{
    // do something
}

TryParse returns a boolean which tells if the try was performed with success or not.

Now, if you want your view to replace incorrect value with a last correct value you need to preserve it in some field and update if needed for example:

private int lastMinValue;
private int lastMaxValue;

private void IntegerUpdown_MouseLeave_Event(object source, EventArgs args)
{
    // use last value as a default for min and max
    int min = lastMinValue;
    int max = lastMaxValue;

    // try to update min and max
    if (int.TryParse(minUpdown.Text?.ToString(), out min))
    {
        lastMinValue = min;
    }
    if (int.TryParse(maxUpdown.Text?.ToString(), out max))
    {
        lastMaxValue = max;
    }

    if (min < max)
    {
        // do something
    }
}
Jacek
  • 829
  • 12
  • 31
  • Thank you, the first works, already it was an answer and it was deleted ( but not for empty string it still gives the same exception), for the second answer it is not possible to get the `lastMinValue`, I will tell you why. Because the user may put for the first time value and change another value and then enter a character , so in this case I can only get the first value not the previous and this a problem for me –  Apr 15 '21 at 12:59
  • 1
    I don't get it. I assume that your `IntegerUpDown` is initialized with certain value, for example '0'. Then user types '100' by hand, then types 'a'. When user moves his mouse out of the control then 'a' should be replaced by '100'. Where's the issue here? – Jacek Apr 15 '21 at 13:24
  • And for null value (note the difference between empty string and null value) you need to use null-conditional operator (`?` character) which returns `null` earlier so that chained method is not invoked on null object. See updated answer. – Jacek Apr 15 '21 at 13:29
  • I will accept your answer but I have seen an issue with this event, so I have changed the whole work to the confirm button, the `MouseLeave_Event` is not good because it is not practical and can be work only if the mouse is inside the `IntegerUpDown` control, what if is it outside?? => the event will not be fired so in this case will cause a big problem for the user. –  Apr 15 '21 at 14:21
  • you can also read about `focus lost` event, this is usually the event that's widely used to confirm entered values in many UI technologies. – Jacek Apr 15 '21 at 22:34