Input string was not in a correct format

The error means that the string you’re trying to parse an integer from doesn’t actually contain a valid integer.

It’s extremely unlikely that the text boxes will contain a valid integer immediately when the form is created – which is where you’re getting the integer values. It would make much more sense to update a and b in the button click events (in the same way that you are in the constructor). Also, check out the Int.TryParse method – it’s much easier to use if the string might not actually contain an integer – it doesn’t throw an exception so it’s easier to recover from.

Leave a Comment