Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I want that my app, when someone enters a letter, or sometehing that isn't a number, shows this message: "You must enter a number!"
C#
void Button_guessClick(object sender, EventArgs e)
{
    try
    {
        int guess = Convert.ToInt32(textBox_guess.Text);
    }
    catch(FormatException ex)
    {
    	MessageBox.Show("You must enter a number!");
    }

    if (guess > number)
    {
    	MessageBox.Show("The number is smaller!");
    }
    
    if (guess < number)
    {
    	MessageBox.Show("The number is bigger!");
    }
    
    if (guess == number)
    {
    	MessageBox.Show("Bravo, you have guessed it");
    	Application.Exit();
    }
}
Posted
Updated 7-Jun-14 23:42pm
v3

It's not exactly clear to me what went wrong with you, but I think that the base of your approach is wrong...
You should not use try...catch - it's a very expensive writing...
Do use Int32.TryParse[^] to check if the value is numeric...
 
Share this answer
 
Comments
OriginalGriff 8-Jun-14 6:30am    
Thank you!
But you missed the typo... :laugh:
Kornfeld Eliyahu Peter 8-Jun-14 6:56am    
I'm full of typo myself - can't get care of yours too...
OriginalGriff 8-Jun-14 6:58am    
Damn!
There was me hoping I'd got myself a spell check wallah! :laugh:
Kornfeld Eliyahu Peter 8-Jun-14 6:58am    
OT - a few weeks now I do not get notifications on such comments like yours here. Did you encountered such a problem (it makes me hard to follow Q&A)...
(Reported on Bugs - twice I think)
OriginalGriff 8-Jun-14 7:00am    
Yes - Same thing, and I've reported it as well.
There do seem to be quite a few fairly serious bugs creeping in here: I think the hamsters have been paying too much attention to Workspaces and not enough to what they are doing with maintenance...
Why not do it properly, instead of using exceptiosn as part of your "normal operation" processing?
C#
int guess;

if (!int.TryParse(textbox_guess.Text, out guess))
   {
   MessageBox.Show("You must enter a number!");
   }
else
   {
   if (guess > number...


[edit]Indentation reverted - OriginalGriff[/edit]
 
Share this answer
 
v5
problem is after you get exception you are not exist the function. then other validations will perform and you will get some other messages as well.

C#
try{
int guess = Convert.ToInt32(textBox_guess.Text);
}

catch(FormatException ex){
    MessageBox.Show("You must enter a number!");
    return; // this will return from your function no more messages will show..
}


you better use Int32.TryParse as Other answers pointed out.
 
Share this answer
 
Comments
OriginalGriff 8-Jun-14 5:38am    
Did I ask you to re-indent my code for me?
No.

So leave it alone. It may not be your preferred style, but that does not mean you should impose yours on others.
DamithSL 8-Jun-14 6:01am    
:) never do it again, by the way, you have code line out side of the coding block. I don't need to comment and say this minor mistake to highest reputation holding person here in CP. That's why I have edited. Sorry if I did something wrong
DamithSL 8-Jun-14 6:54am    
Now some one else edit the same post, you say Thanks :-)
OriginalGriff 8-Jun-14 6:57am    
Yes.
Because his change was sensible, yours was gratuitous! :laugh:
DamithSL 8-Jun-14 7:11am    
lesson learned
Convert.ToInt does not check if an input is really an integer or not.
To check type, use a Int.TryParse.

To be cautious about cultures, go through Check If A String Value Is Numeric[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900