What am I missing here:
if (textBox_End_Miles.Text != "" && Convert.ToInt32(textBox_End_Miles.Text) > Convert.ToInt32(label_Start_Miles1.Text))
I keep getting a runtime error any time I modify the textbox.
What am I missing here:
if (textBox_End_Miles.Text != "" && Convert.ToInt32(textBox_End_Miles.Text) > Convert.ToInt32(label_Start_Miles1.Text))
I keep getting a runtime error any time I modify the textbox.
Maybe I should add more code and a description.
private void textBox_End_Miles_TextChanged(object sender, EventArgs e)
{
if (textBox_End_Miles.Text != "" && Convert.ToInt32(textBox_End_Miles.Text) > Convert.ToInt32(label_Start_Miles1.Text))
label_Miles_Drove1.Text = (System.Convert.ToInt32(textBox_End_Miles.Text) - System.Convert.ToInt32(label_Start_Miles1.Text)).ToString();
else
label_Miles_Drove1.Text = "0";
}
I want the "Miles Drove" label to automatically update when (a) the text in "Ending Miles" is not "", and (b) the text in the "Ending Miles" is > the text in "Starting Miles". I"m getting this error when I start typing:
Input string was not in a correct format.
Is there a better way to do this? I want to make sure only numeric values are entered, but I'm not sure how without using a maskedTextBox. I can't get it to work either way right now though (i.e., my test input is "4").
maybe add some try catch logic.
if it cant be converted your try will fail. then write 0. if it is converted then write your if condition in the try, if value is greater than other value and write the number or 0. so 3 paths it can take.
here is some code off msdn showing convert in a try. if the convert fails you go immediately to the catch, if it passes you never reach catch. you can use a regular text box then.
string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00",
" 0", "137", "1601.9", Int32.MaxValue.ToString() };
int result;
foreach (string value in values)
{
try {
result = Convert.ToInt32(value);
Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.",
value.GetType().Name, value, result.GetType().Name, result);
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Int32 type.", value);
}
catch (FormatException) {
Console.WriteLine("The {0} value '{1}' is not in a recognizable format.",
value.GetType().Name, value);
}
}
// The example displays the following output:
// The String value 'One' is not in a recognizable format.
// The String value '1.34e28' is not in a recognizable format.
// The String value '-26.87' is not in a recognizable format.
// Converted the String value '-18' to the Int32 value -18.
// The String value '-6.00' is not in a recognizable format.
// Converted the String value ' 0' to the Int32 value 0.
// Converted the String value '137' to the Int32 value 137.
// The String value '1601.9' is not in a recognizable format.
// Converted the String value '2147483647' to the Int32 value 2147483647.
Try keeping a converted value for arithmetic tests and use TryParse when doing convertions.
int Start_Miles1_Value = 0;
int End_Miles_Value = 0;
private void textBox1_TextChanged(object sender, EventArgs e)
{
int retVal = 0;
if (Int32.TryParse(label_End_Miles.Text, out End_Miles_Value) &&
(End_Miles_Value > Start_Miles1_Value))
{
retVal = End_Miles_Value - Start_Miles1_Value;
}
label_Miles_Drove1.Text = retVal.ToString();
}
Also, although not needed when using TryParse, I tend to use string.IsNullOrEmpty when testing for a blank string; both null and Empty often result in the same runtime faults.
Great, thanks. The only problem is, my label is tied to a database - I think that may be what's causing the issue. Every time I try to convert the Start_Miles1 label, it gives me errors. During debug, it shows the value as "10,000" (pulled from db).
How and where are you converting it and what errors are you gettings?
I just tried:
int Start_Miles_Value = Convert.ToInt32(label_Start_Miles1.Text);
and I get the same Input string was not in a correct format error.
This label is tied to a db. During debug, it shows "10,000" as the text.
Try this.
int Start_Miles_Value = 0;
private void button8_Click(object sender, EventArgs e)
{
if (Int32.TryParse(label_Start_Miles1.Text, System.Globalization.NumberStyles.Number, null, out Start_Miles_Value))
MessageBox.Show(string.Format("OK - Value is {0}", Start_Miles_Value));
else
MessageBox.Show("NOT OK");
}
That works fine - it shows the value as 10000.
Was able to modify your code to get it working. Thanks for the help.
private void textBox_End_Miles_TextChanged(object sender, EventArgs e)
{
int End_Miles_Value = Convert.ToInt32(textBox_End_Miles.Text);
int Start_Miles_Value;
if (Int32.TryParse(label_Start_Miles1.Text, System.Globalization.NumberStyles.Number, null, out Start_Miles_Value) && End_Miles_Value > Start_Miles_Value)
label_Miles_Drove1.Text = (End_Miles_Value - Start_Miles_Value).ToString();
}
I'd recommend using TryParse for textBox_End_Miles.Text too.
Try typing in "abcd" and see what happens.
I only made it to "a" :)
I'll change that now - thanks.
Another quick question in regards to this. Here's my code:
private void textBox_End_Miles_TextChanged(object sender, EventArgs e)
{
int End_Miles_Value;
int Start_Miles_Value;
//Checks for valid data within label_Start_Miles1.Text and textBox_End_Miles.Text
//If values are valid (numerical) and if Ending Miles is more than Starting Miles, update the Miles_Drove label
if (!Int32.TryParse(label_Start_Miles1.Text, System.Globalization.NumberStyles.Number, null, out Start_Miles_Value)
|| !Int32.TryParse(textBox_End_Miles.Text, System.Globalization.NumberStyles.Number, null, out End_Miles_Value))
{
MessageBox.Show("Numerical Data Only.", "Invalid Miles Value", MessageBoxButtons.OK);
textBox_End_Miles.ResetText();
label_Miles_Drove1.ResetText();
}
else if (End_Miles_Value > Start_Miles_Value)
label_Miles_Drove1.Text = (End_Miles_Value - Start_Miles_Value).ToString();
}
When I do the textBox_End_Miles.ResetText(); it triggers the Text_Changed event, and shows the MessageBox twice. Is there a way around this?
Detach and re-attach the TextChanged event handler using -= and +=.
textBox_End_Miles.TextChanged -= new system.EventHandler(textBox_End_Miles_TextChanged);
textBox_End_Miles.ResetText();
textBox_End_Miles.TextChanged += new system.EventHandler(textBox_End_Miles_TextChanged);
Awesome. Thanks! What happens if you += more than once? Will it throw it x-times every time?
No. It doesn't even throw an exception.
Nor does -= if you have not done += before.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.