private void ButtonGuess_Click(object sender, EventArgs e) { int userGuess; if (int.TryParse(TextBoxGuess.Text, out userGuess)) { /* Check can 0: num is lower, 1: num is higher, * 2: guess is correct! */ int guessCheck = Game.CheckGuess(userGuess); if (guessCheck == 2) { /* Pop a message box and reset game */ MessageBox.Show("Correct! " + "New random number generated"); Game.GenerateNewNumber(); TextBoxGuess.Clear(); // Clear guess textbox } else { string text = (guessCheck == 0) ? "lower!" : "higher"; textBox1.Text = string.Format( "You guessed {0}\r\nThe number is {1}", userGuess, text); } /* Select guess textbox and * focus text cursor */ TextBoxGuess.Focus(); TextBoxGuess.SelectAll(); } }
public GuessNumber() { InitializeComponent(); Game.GenerateNewNumber(); TextBoxGuess.Text = "0"; TextBoxGuess.Focus(); TextBoxGuess.SelectAll(); textBox1.Select(0, 0); }