0

I created a guessing game application. When the user enters a letter it crashes. I want the textbox to only have integers. I was trying to create a validation so when the user enters a letter it will display a message only integers only. How can I add a validation to the program so where the user can only enter an integer?

I tried using this code but it still crashes:

double input = Convert.ToInt32(txtInput.Text);

if (Double.TryParse(txtInput.Text, out input))
{
    txtInput.Text = Convert.ToString(input);
}
else 
{
    MessageBox.Show("Please enter a valid number.");
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        double guess;

        private void Form1_Load(object sender, EventArgs e)
        {
            Random rand = new Random();
            guess = rand.Next(1, 101);
        }

        private void Clickbutton_click(object sender, EventArgs e)
        {
            double input = Convert.ToInt32(txtInput.Text);

            if (Double.TryParse(txtInput.Text, out input))
            {
                txtInput.Text = Convert.ToString(input);
            }
            else
            {
                MessageBox.Show("Please enter a valid number.");
            }

            if (input == guess)
            {
                lblMessage.Text = "You Are Corrrect, You Win" + " " +
                    " \r\n Random Number was =  " + Convert.ToString(guess);
            }
            else if (input > guess)
            {
                lblMessage.Text = "Number is too High, Try Again";
                txtInput.Clear();
            }
            else if (input < guess)
            {
                lblMessage.Text = "Number is too Low, Try Again";
                txtInput.Clear();
            }
        }
    }
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
BiggieHip
  • 1
  • 1

2 Answers2

0

You can use the NumericUpDown control.

The advantage of this control, is that it does not only has a text box, but also only numbers can be entered, and there are two small buttons on the right to increase with 1 or decrease with 1 (at least by default).

Also, more properties can be set.

The official documentation can be found here

And below is an example how it looks:

enter image description here

At codeproject an extended version can be found.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

Besides the answer with the NumericUpDown. Just something to add. With some little adjustments your code will work.

private void Clickbutton_click(object sender, EventArgs e)
{
        //// Convert.ToInt32 throws an exception if it fails 
        //// to parse the string to an integer
        //// So you don't want to parse the input directly
        ////double input = Convert.ToInt32(txtInput.Text);


        // Your second attempt is the goal
        double input;
        if (!Double.TryParse(txtInput.Text, out input))
        {
            MessageBox.Show("Please enter a valid number.");
            return; //// We are done. Nothing more to do here
        }

        txtInput.Text = input.ToString();


        if ( input == guess)
        {
            lblMessage.Text = "You Are Corrrect, You Win" + " "+
                " \r\n Random Number was =  " + Convert.ToString(guess);
        }
        else if (input > guess)
        {
            lblMessage.Text = "Number is too High, Try Again";
            txtInput.Clear();

        }
        else if (input < guess)
        {
            lblMessage.Text = "Number is too Low, Try Again";
            txtInput.Clear();
        }
   }     
Link
  • 1,307
  • 1
  • 11
  • 23