Unable to Disable Button when text field empty VS2015

Ok, learning how to use VS2015, creating Flash Card Math game using Windows Forms for my kid, everything is working, I have a game where they chose the number, how many questions and they get get random numbers to answer multiplication questions. Now I am working on making sure the users can't break the application and I want to disable a button until someone has entered a valid number, I have disabled all keys except numbers, but if they click the button and nothing is in box it blows up:

Example of code to disable all characters except numbers:
1
2
3
4
5
6
7
 private: System::Void textBox2_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {

	{
		// Accept only digits and the Backspace character
		if (!Char::IsDigit(e->KeyChar) && e->KeyChar != 0x08)
			e->Handled = true;
	}


Here is what I tried to use for the Text Box and also in the section for the button I want to disable:
1
2
3
4
5
6
7
8
9
	if (Convert::ToInt32(textBox2->Text) > 0)
	{
		button3->Enabled = true;
	}
	else
	{
		
		button3->Enabled = false;
	}
Last edited on
Use Int32::TryParse() -- rather than Convert::ToInt32() -- as it doesn't get upset if the conversion isn't possible. Or even better, as you only want to deal with positive numbers (?), UInt32::TryParse()

1
2
3
4
	private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) {
			unsigned int value = 0;
			button3->Enabled = UInt32::TryParse(textBox2->Text, value);
		}


Andy
Last edited on
Topic archived. No new replies allowed.