C++ Windows Forms application unhandled exception error when textbox empty

Posted by cmorris1441 on Programmers See other posts from Programmers or by cmorris1441
Published on 2012-09-30T15:37:36Z Indexed on 2012/09/30 15:48 UTC
Read the original article Hit count: 229

Filed under:
|

I'm building a temperature conversion application in Visual Studio for a C++ course. It's a Windows Forms application and the code that I've written is below. There's other code to of course, but I'm not sure you need it to help me.

My problem is, when I run the application if I don't have anything entered into either the txtFahrenheit or txtCelsius2 textboxes I get the following error:

"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"

The application only works right now when a number is entered into both of the textboxes.

I was told to try and use this:

Double::TryParse()

but I'm brand new to C++ and can't figure out how to use it, even after checking the MSDN library.

Here's my code:

private: System::Void btnFtoC_Click(System::Object^  sender, System::EventArgs^  e) 
             {               
                // Convert the input in the Fahrenheit textbox to a double datatype named fahrenheit for manipulation
                double fahrenheit = Convert::ToDouble(txtFahrenheit->Text);
                // Set the result string to F * (5/9) -32
                double result = fahrenheit * .5556 - 32;
                // Set the Celsius text box to display the result string
                txtCelsius->Text = result.ToString();
             }

private: System::Void btnCtoF_Click(System::Object^  sender, System::EventArgs^  e) 
             {
                // Convert the input in the Celsius textbox to a double datatype name celsius for manipulation
                double celsius = Convert::ToDouble(txtCelsius2->Text);
                // Set the result2 string to C * (9/5) + 32
                double result2 = celsius * 1.8 + 32;
                // Set the Fahrenheit text box to display the result2 string
                txtFahrenheit2->Text = result2.ToString();
             }

© Programmers or respective owner

Related posts about c++

Related posts about winforms