每当我的表单1尝试打开表单2时,我都会收到此错误...
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
这是什么意思?这是我用来打开Form2的代码......
if (TimerValue == 5000)
{
this -> timer1 -> Stop();
TimerValue = 0;
Form2 ^ f2 = gcnew Form2;
f2->Show();
MoviePlay = 1;
StreamWriter^ outFile = gcnew StreamWriter("Movie.txt");
String^ Movie = MoviePlay.ToString();
outFile->Write(Movie);
outFile->Close();
this -> button4 ->Visible = false;
this -> button5 ->Visible = false;
this -> label2 ->Visible = false;
this -> button2 ->Visible = false;
this -> button3 ->Visible = false;
FriendString = File::ReadAllText(".\\Rca13\\Friend.txt");
Friend = System::Convert::ToInt32(FriendString);
if (Friend == 1)
{
this -> pictureBox1 -> Load("RetaliationBackground1_Pony1");
}
if (Friend == 2)
{
this -> pictureBox1 -> Load("RetaliationBackground1_Pony2");
}
if (Friend == 3)
{
this -> pictureBox1 -> Load("RetaliationBackground1_Pony3");
}
if (Friend == 4)
{
this -> pictureBox1 -> Load("RetaliationBackground1_SwagMasta");
}
}
此代码中是否有任何内容导致错误?
答案 0 :(得分:3)
我怀疑问题是这样的:
Friend = System::Convert::ToInt32(FriendString);
我的猜测是FriendString
不是有效整数。如果这是用户输入,您应该使用Int32.TryParse
,这样您就可以在不诉诸异常的情况下检测到这一点。
学习读取堆栈跟踪以查看哪个方法失败以及 代码中的最后一个位置是非常重要的......然后进一步诊断出问题。