Windows CLR app breaks because of Text Box

So I am making very simple app and I have a text box where the user enters a number, I convert the string in the text box to a number by doing the following:

private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {

answer = System::Convert::ToInt32(textBox1->Text);



Then I have an if statement and that works, but but when I go to enter a new number in the text box it already has a zero and if I delete it breaks application, and if I use Clear(), it breaks at that point as well, see below:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
guessCount = (guessCount + 1);
label2->Text = guessCount.ToString();

if (answer == 3)
{
label3->Text = "Nice Job";
textBox1->Hide();
button1->Hide();
button2->Text = "Click Here to Play Again";
this->button2->Enabled = true;
button2->Show();

}

if (answer != 3)
{
//answer = 0;
//this->textBox1->Clear();
//this->textBox1->Clear();
this->textBox1->Text = "0";
}

What am I missing, why cannot I use Clear()?
Please use code tags when providing code, it helps tremendously while reading the code. To format you post with code tags click the <> icon just under the word "Format:" on the side of the edit box.

As for the problem, you are converting the value to an int every time the text is changed; therefore, you delete the '0' is changing the text and invoking a call to

textBox1_TextChanged

The computer can not convert a blank character into an integer.
Thanks for responding and I will make sure to put my code in the correct format.

I was making it too difficult and creating variables that are not needed, here is how it works, next I will be working on randomizing the number:

removed all code from this box
1
2
3
4
	
private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
	
	}


Updated this section to use label instead of int variable, I am sure this causes other problems but will see. :0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
		guessCount = (guessCount + 1);
		label2->Text = guessCount.ToString();

		if (textBox1->Text == "3")
		{
			label3->Text = "Nice Job";
			textBox1->Hide();
			button1->Hide();
			button2->Text = "Click Here to Play Again";
			this->button2->Enabled = true;
			button2->Show();

		}

		if (textBox1->Text != "3")
		{
			this->textBox1->Clear();
		}
}
Topic archived. No new replies allowed.