C++ Windows Forms application unhandled exception

2019-03-06 02:22发布

问题:

I'm building a temperature conversion application in Visual Studio for a C++ course. It's a Windows Forms application.

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.

回答1:

This will check that the entry in your textbox is convertible to a number.

double val;
bool result = System::Double::TryParse(txtFahrenheit->Text,val);

if (result)
{
    //Converted successfully, you can use val
}
else
{
    //Error
}