-1

Whenever My form 1 tries to open form 2 I get this error...

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

Additional information: Input string was not in a correct format.

What does this mean? Here is the Code I use for opening 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");
             }

         }

Is there anything that was put into this code causing the errors?

4

1 回答 1

3

Well I suspect the problem is this:

Friend = System::Convert::ToInt32(FriendString);

My guess is that FriendString isn't a valid integer. If this is user input, you should probably use Int32.TryParse so that you can detect this without resorting to exceptions.

It's important to learn to read stack traces to see which method has failed and the last place in your code that's mentioned... and then diagnose the prolem further.

于 2013-06-03T19:00:23.573 回答