TextBox: Conversion from String to Double
-
Hi, I am new to CLI-Windows Forms programming and I need to perform this simple task: 1) Allow the user to enter a value in a Text Box (textBox1) 2) Assign such value to a variable of type double (variable1) [I am having difficulties casting the string to double] 3) Perform computations on that variable [I should be able to do this once it's a double] 4) Display the resulting value in another Text Box (textBox2) Can someone be so kind to help me with the correct syntax I should use for each step? Many thanks.
-
Hi, I am new to CLI-Windows Forms programming and I need to perform this simple task: 1) Allow the user to enter a value in a Text Box (textBox1) 2) Assign such value to a variable of type double (variable1) [I am having difficulties casting the string to double] 3) Perform computations on that variable [I should be able to do this once it's a double] 4) Display the resulting value in another Text Box (textBox2) Can someone be so kind to help me with the correct syntax I should use for each step? Many thanks.
For step 2: Double.Parse Method[^] Double.TryParse Method[^] For step 4: Double.ToString Method[^]
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi, I am new to CLI-Windows Forms programming and I need to perform this simple task: 1) Allow the user to enter a value in a Text Box (textBox1) 2) Assign such value to a variable of type double (variable1) [I am having difficulties casting the string to double] 3) Perform computations on that variable [I should be able to do this once it's a double] 4) Display the resulting value in another Text Box (textBox2) Can someone be so kind to help me with the correct syntax I should use for each step? Many thanks.
-
J_E_D_I wrote:
Hi, I am new to CLI-Windows Forms programming and I need to perform this simple task:
led mike
Awesome! :-D
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
For step 2: Double.Parse Method[^] Double.TryParse Method[^] For step 4: Double.ToString Method[^]
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks for the advice, it works. But I obviously get a crash when I leave the text box empty or I enter a non numerical value. Here is the code I use to assign the value entered in the text box (as string) to a type double variable: // variable1 = double::Parse(textBox1->Text); // How can I solve this? Thanks!
-
Thanks for the advice, it works. But I obviously get a crash when I leave the text box empty or I enter a non numerical value. Here is the code I use to assign the value entered in the text box (as string) to a type double variable: // variable1 = double::Parse(textBox1->Text); // How can I solve this? Thanks!
J_E_D_I wrote:
But I obviously get a crash when I leave the text box empty or I enter a non numerical value.
Use TryParse(). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Awesome! :-D
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
J_E_D_I wrote:
But I obviously get a crash when I leave the text box empty or I enter a non numerical value.
Use TryParse(). Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I have tried: // double number; variable1 = double::TryParse(textBox1->Text, number); // With the code above it's better and the program does not crash anymore. However the problem is not completely solved because when I leave the text box empty or enter a character different from a number, the result of variable1 is zero, which causes an error in the calculation that follows. How can I show an "Error" message when this has happened? Alternatively, is there any way that I could make the textBox accept only numerical values and display an error in the other cases?
-
I have tried: // double number; variable1 = double::TryParse(textBox1->Text, number); // With the code above it's better and the program does not crash anymore. However the problem is not completely solved because when I leave the text box empty or enter a character different from a number, the result of variable1 is zero, which causes an error in the calculation that follows. How can I show an "Error" message when this has happened? Alternatively, is there any way that I could make the textBox accept only numerical values and display an error in the other cases?
TryParse returns a bool.
double variable1;
if (double::TryParse(textBox1->Text, variable1))
{
// parse succeeded - safe to use use variable1
}
else
{
// parse failed
}Did your Visual Studio not come with MSDN library? :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Indeed! :cool:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
TryParse returns a bool.
double variable1;
if (double::TryParse(textBox1->Text, variable1))
{
// parse succeeded - safe to use use variable1
}
else
{
// parse failed
}Did your Visual Studio not come with MSDN library? :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
This is a fantastic answer! Thank you so much, it solved the problem.
Mark Salsbery wrote:
Did your Visual Studio not come with MSDN library?
Probably you guys super expert programmers manage to find what you are looking for in MSDN. I program in my spare time just for fun and I do not find them helpful AT ALL!! :sigh: They seriously lack of simple examples for beginners like me and they take too much for granted. Regarding TryParse there was no specific example for c++. Cheers!
-
This is a fantastic answer! Thank you so much, it solved the problem.
Mark Salsbery wrote:
Did your Visual Studio not come with MSDN library?
Probably you guys super expert programmers manage to find what you are looking for in MSDN. I program in my spare time just for fun and I do not find them helpful AT ALL!! :sigh: They seriously lack of simple examples for beginners like me and they take too much for granted. Regarding TryParse there was no specific example for c++. Cheers!
J_E_D_I wrote:
there was no specific example for c++
Hint: Use the C# examples. For most examples of using .NET framework classes, the only difference will be '->' in c++ instead of '.' in c# gcnew in c++ instead of new in c#. Cheers! :beer: Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: