I need to convert a the string of a Text box to a Value
-
You mean
TryParse
;Parse
returns the parsed value or throws an exception. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
What value is in the textbox? If it can't be parsed as a
short
using the current culture settings, thenConvert.ToInt16
will throw an exception. You should use TryParse[^] rather than theConvert
method to avoid the "can't parse this" exception:if (int.TryParse(textBox1.Text, out int value))
{
if (value > 1000)
{
MessageBox.Show("Here!");
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Hmmm, I'm reading back a sensor, it's giving values all over the show!
private void button1_Click(object sender, EventArgs e)
{
//int value = 0;// MessageBox.Show(textBox1.Text);
if (int.TryParse(textBox1.Text, out int value)) { if (value > 1000) { MessageBox.Show("Here!"); } MessageBox.Show(value.ToString()); } MessageBox.Show(value.ToString());
}
value is taken as 0, despite textBox1.Text having 1234.56...
-
Hmmm, I'm reading back a sensor, it's giving values all over the show!
private void button1_Click(object sender, EventArgs e)
{
//int value = 0;// MessageBox.Show(textBox1.Text);
if (int.TryParse(textBox1.Text, out int value)) { if (value > 1000) { MessageBox.Show("Here!"); } MessageBox.Show(value.ToString()); } MessageBox.Show(value.ToString());
}
value is taken as 0, despite textBox1.Text having 1234.56...
-
Hmmm, I'm reading back a sensor, it's giving values all over the show!
private void button1_Click(object sender, EventArgs e)
{
//int value = 0;// MessageBox.Show(textBox1.Text);
if (int.TryParse(textBox1.Text, out int value)) { if (value > 1000) { MessageBox.Show("Here!"); } MessageBox.Show(value.ToString()); } MessageBox.Show(value.ToString());
}
value is taken as 0, despite textBox1.Text having 1234.56...
Maybe try decimal.TryParse()? as the input is not an int.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
glennPattonWork3 wrote:
despite textBox1.Text having 1234.56...
But 1234.56 is a
Float/Double
not anInt
; the two are quite different.You are right, but even if make 1234 it still returns a 0...
-
You are right, but even if make 1234 it still returns a 0...
-
Maybe try decimal.TryParse()? as the input is not an int.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
Thanks, that appears to work! :-D
-
You are right, but even if make 1234 it still returns a 0...
Did the TryParse call succeed, return true?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Come on Glen, you need to show us the exact code, and the exact text that you are using.
Hi, This is the code I have been trying to get all afternoon!
private void button1_Click(object sender, EventArgs e)
{
float ForceVal = 0;
//MessageBox.Show(weightTxt.Text);
//ForceVal = Convert.ToDecimal(weightTxt.Text);//int.TryParse(weightTxt.Text, out int value); decimal.TryParse(weightTxt.Text, out decimal value); // MessageBox.Show(value.ToString()); if (value > 1000) { MessageBox.Show("Here!"); } MessageBox.Show(value.ToString()); }
I think that should do it. I'm hacking together a test program that interfaces to a Phidget load cell in trying to measure a value that needs to converted to Newtons from Grams (I think!). I hate the fact I wasted so much time trying to get the value to be used out of a darned label. I haven't really done much Windows code (I'm an embedded guy really) since VS2008 was the new kid on the block and times like these it shows! :sigh:
-
Did the TryParse call succeed, return true?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
Sort of I had to:
decimal.TryParse(weightTxt.Text, out decimal value);
as I was messing with a floating point number, haven't coded in Windows for a while, man it shows!
-
Thanks, that appears to work! :-D
:thumbsup: Good news!
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle