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:
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()