Textbox 'Input string was not in a correct format.' exception from "-"

SageT

Member
Joined
Feb 2, 2022
Messages
10
Programming Experience
3-5
I need to input an negative number into a textbox. as soon as the "-" key is pressed I get an 'Input string was not in a correct format.' exception.
What is the work around for this?
 
Without seeing your code and you telling us where the exception is being thrown, we can only guess.

Right now, I'm guessing that you are using the textbox the wrong way and watching every keypress and then using Convert.Int32 on the current contents of the textbox.

That is the absolutely wrong thing to do.

The correct thing to do is way for the user to click on a button on the form thta contains your textbox and then try to process the text that was entered. Next, only amateurs or people trying to show demonstration code use Convert. Production code uses TryParse() so that an exception is not thrown, but they are notified that there was a parsing error when trying to convert the text into a value. Pros don't use try-catch for flow control, or for non-exceptional conditions.
 
Ok, so if I understand what you are saying... do not process data in the code created by the application, but wait until I need to process and use the values. At that time check for valid inputs. Then send the user back to change the contents?

private void PlotMin_TextChanged(object sender, EventArgs e)
{
double temp = Convert.ToDouble(PlotMin.Text);

if (temp < -55)
{
MessageBox.Show("Selected temprature is too low, Min is -55");
}
else
{
Form5.Plot_Min = temp;
}

}

In several years looking at code I have only seen try-catch and never TryParse. I guess that the companies paying them over $150K+ year are fools for not hiring "Pros".

Me I am not that good and have not written serious code for about 10 years and just trying Forms now.
Still thanks, your help on my last problem was a life saver.
 
Last edited:
The issue should be obvious. You're validating the user's input before they have finished entering that input. There's no guarantee that partial input will be valid when the final input would be. Don't validate the data until the user has entered all the data. That's why controls have a Validating event. You validate input in the handler for that event.
 
In several years looking at code I have only seen try-catch and never TryParse. I guess that the companies paying them over $150K+ year are fools for not hiring "Pros".
You got that right. Anyone who doesn't use TryParse when validating and converting relevant data in .NET code is a bad developer. They may still do some good things, but there's really no excuse for not knowing that TryParse is available and why it's better.
 
Thanks. Again I was not aware that Textbox would try to process each key stroke. As I said, very new to Forms.
 
I was not aware that Textbox would try to process each key stroke.
You should have been though, regardless of how new you are. Think it through. The TextChanged event is raised whenever the Text property value changes. Of course the Text property changes each time you type a character into the control. This is the sort of stuff that you need to think about. Don't just wait to be told. Ask yourself "what if...".
 
Back
Top Bottom