using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
Guess_that_Number{
public partial class Form1 : Form{
//Data Declarations section int Begin,End,MissTry,Guess,range,veryHigh, High, closePos,closeNeg, Low, veryLow; public Form1(){
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e){
}
private void lblGuess_Click(object sender, EventArgs e){
}
private void btnGuess_Click(object sender, EventArgs e){
//Local Variable int random; //Variable initailizationsveryHigh = random +20;
High = random +10;
closePos = random +5;
closeNeg = random -5;
Low = random -10;
veryLow = random -20;
MissTry =1;
//Counter for missed triesRandom rand = new Random(DateTime.Now.Second);
random = rand.Next(Begin, End);
//Loop below is used to catch invalid input while (Guess < Begin && Guess > End)
{
txtHint.Text =
"You've entered " + Guess + " which is outside of the guessing range. Try again!";txtGuess.Clear();
//Clears the txtGuess text box}
//Loop below is intended to take the user's guess and determine how close //or far a guess is. do{ if( Guess == random){
txtHint.Text = Guess +
" is the the correct number!";}
else if(Guess <= veryHigh && Guess > High){txtHint.Text=
"Guess is too high. Guess much lower to win.";txtGuess.Clear();
MissTry++;
}
else if (Guess <= High && Guess > closePos){txtHint.Text=
"Guess is high. Guess lower to win.";txtGuess.Clear();
MissTry++;
}
else if(Guess <=closePos && Guess > random){txtHint.Text=
"Guess is close. Guess a little lower to win!";txtGuess.Clear();
MissTry++;
}
else if(Guess >=veryLow && Guess < Low){txtHint.Text=
"Guess is too low. Guess much higher to win!";txtGuess.Clear();
MissTry++;
}
else if(Guess >=Low && Guess < closeNeg){txtHint.Text=
"Guess is low. Guess higher to win!";txtGuess.Clear();
MissTry++;
}
else if (Guess >=closeNeg && Guess < random){txtHint.Text=
"Guess is close. Guess a little higher to win!";txtGuess.Clear();
MissTry++;
}
}
while( Guess != random || MissTry < 4);}
private void txtNum1_TextChanged(object sender, EventArgs e)
{
Begin =
int.Parse(txtNum1.Text); //Converts input in textbox Num1 to an int}
private void txtNum2_TextChanged(object sender, EventArgs e){
End =
int.Parse(txtNum2.Text);}
private void btnStart_Click(object sender, EventArgs e){
txtRange.Text =
"Guess a number between " + Begin + " to " + End; //Displays the 'guessing' range to the user}
private void txtRange_TextChanged(object sender, EventArgs e){
}
private void txtGuess_TextChanged(object sender, EventArgs e){
Guess =
int.Parse(txtGuess.Text);}
private void txtHint_TextChanged(object sender, EventArgs e){
}
}
}
Can someone PLEASE tell me why the complier keeps telling me that my " Guess = int.Parse(Guess.Text);", statement is in the incorrect format.
AlanPosted Mar 22, 2008, 6:52 AM
One other thing I've noticed is that you're doing the parsing in the TextChanged eventhandler which means that the Text property is reparsed each time you enter a digit.
It also means that if you clear the TextBox to enter another number (by continually pressing backspace or otherwise), then the Text property will become an empty string and int.Parse will then give a format error.
If this in fact is the problem, then you can avoid it with code like this:
private void txtNum1_TextChanged(object sender, EventArgs e)
{
if (txtNum1.Text != "") Begin = int.Parse(txtNum1.Text); //Converts input in textbox Num1 to an int
}
However, it's not the most robust way of doing things and I'd consider delaying the parsing until the whole number has been entered i.e. the TextBox has lost focus.
Incidentally, the only difference between int.Parse and Convert.ToInt32(string) is that the latter doesn't throw an exception if the string is null but returns 0 instead.
Personally, I prefer to use the int.TryParse() method (.NET 2.0 or later) which never throws an exception:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx
Christopher LeggettPosted Mar 21, 2008, 11:27 PM
Ryan AlfordPosted Mar 21, 2008, 9:40 PM
try
{
Guess = Convert.ToInt32(txtGuess.Text);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
Christopher LeggettPosted Mar 21, 2008, 8:31 PM
Alan,
Your response is HIGHLY appreciated. My win form is supposed to allow the user to enter in a start and ending number to guess from, generating a number the token number at random. When I run the win form I input an int with no special characters, yet the complier says that my parsing is incorrect. Any further suggestion will be highly appreciated.
AlanPosted Mar 21, 2008, 8:09 PM
Well, it depends of course on what you're entering into the TextBox.
One reason why the int.Parse() method can fail is if you put thousand separators in the string. So, for example, you need to enter 1000 rather than 1,000 into the TextBox to avoid a format exception being thrown.