Skip to main content
GameDev.net gamedev.net
🔒 Locked

C#: Throw exception, then wait for input

Started by FinalMinuet Mar 8, 2006 at 10:32 PM 8 replies 1.2k views
Original Post
FinalMinuet
FinalMinuet
Ok, in my program, the user needs to enter their user number before continuing on. If they don't enter a valid integer, the program throws an exception. Instead of continuing on, I want the program to wait for the user to try inputting a valid number, then start the try block over again. Here's the code.

int number = 0;
try {
  number = Convert.ToInt32(usernumber.Text);
  usernumber.Text = "";
}
catch {
  MessageBox.Show("You didn't enter a number. Please double-check your input and try again.");
  usernumber.Text = "";
}

user = new User(number);
trial = new Trial(number);
InitializeForms();
StartTrials();

abnormal
abnormal
I guess you want a better solution .. here you go:

int number;while (Int32.TryParse(inputString, out number) == false){	// Get new input} // while (Int32.TryParse)// Use number now
Microsoft DirectX MVP. My Blog: abi.exdream.com
NexusOne
NexusOne
Just use a while loop, with a break statement at the end, but put a "continue" statement at the end of the catch.
Graphics make the game! 8-)
FinalMinuet
FinalMinuet
Visual Studio said it didn't have a definition for Int32.TryParse(), and it won't let me use a continue statement in the catch block.

Let me try a different approach. The user is given a text box and a Start button. They are told to enter their user number, then click Start. If the number is valid, the rest of the program starts. If it's not a number, I want the program to display the error message (works), clear the text box (works), and exit the function -- private void start_Click(object sender, System.EventArgs e) {}. This will basically start the program over without forcing the user to restart the program manually. Make sense?
nprz
nprz
Int32.TryParse() is new to .NET 2.0
Quote:

Let me try a different approach. The user is given a text box and a Start button. They are told to enter their user number, then click Start. If the number is valid, the rest of the program starts. If it's not a number, I want the program to display the error message (works), clear the text box (works), and exit the function -- private void start_Click(object sender, System.EventArgs e) {}. This will basically start the program over without forcing the user to restart the program manually. Make sense?

private void start_Click(object sender, System.EventArgs e){   try   {      int num = Int32.Parse(TextBox1.Text);      // do whatever you want with the number.   } catch (FormatException)   {      MessageBox.Show("You need to enter a number.");      TextBox1.Text = "";      return;   }}
SnprBoB86
SnprBoB86
Instead of:

TextBox1.Text = "";


Do:

TextBox1.SelectAll();
TextBox1.Focus();



Simply clearing the text box might be an annoyance if the user had typed a MOSTLY valid number, but accidentally had an extra character in there. My solution selects all of the text, so that the user can quickly overwrite it with a real number or use the arrow keys to move to the error and correct it. Additionally, I set focus to the control so that the user doesn't need to reach for their mouse.
FinalMinuet
FinalMinuet
Perfect. Many thanks to all of you!
SnprBoB86
SnprBoB86
RE: Validating event

I tend to dissagree with this mentality.
Modern GUI applications must be multi-tasking aware. If the user clears the field and then realizes he wants to go copy paste the number from some other application, a Validation event now forces him to input some garbage value. Even worse, the field is now not empty, so when he returns, he might overlook it, click "OK" and have performed some incorrect action.
SnprBoB86
SnprBoB86
RE: RE: Validating event

I forgot to mention when to use the validating event!

You use it when it is an error for a field to not have a value.

See the properties pane in VS.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.