C++ C++;文本框为空时Windows窗体应用程序未处理的异常错误

C++ C++;文本框为空时Windows窗体应用程序未处理的异常错误,c++,winforms,c++-cli,C++,Winforms,C++ Cli,可能重复: 我在VisualStudio中为C++课程构建了一个温度转换应用程序。这是一个Windows窗体应用程序,下面是我编写的代码。当然还有其他代码,但我不确定您是否需要它来帮助我 我的问题是,当我运行应用程序时,如果我没有在txtFahrenheit或txtCelsius2文本框中输入任何内容,我会得到以下错误: “mscorlib.dll中发生类型为'System.FormatException'的未处理异常” 只有在两个文本框中都输入了一个数字时,应用程序才会立即工作 privat

可能重复:

我在VisualStudio中为C++课程构建了一个温度转换应用程序。这是一个Windows窗体应用程序,下面是我编写的代码。当然还有其他代码,但我不确定您是否需要它来帮助我

我的问题是,当我运行应用程序时,如果我没有在txtFahrenheit或txtCelsius2文本框中输入任何内容,我会得到以下错误:

“mscorlib.dll中发生类型为'System.FormatException'的未处理异常”

只有在两个文本框中都输入了一个数字时,应用程序才会立即工作

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();
             }

不能将空字符串转换为
双精度


您应该使用
if
语句来执行其他操作。

使用Double::TryParse()代替。@HansPassant代替what?这是我的第一个C++程序,我以前没有听说过。我改成了这个双华氏温度=double::TryParse(txtfhrenheit->Text);但我现在又犯了一个错误。但我可能实现错了。避免只是随机尝试代码。使用MSDN库查找Double.TryParse方法定义。它有两个论点。