Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Validate numeric textbox using int.tryparse visual C#.NET

3.67/5 (3 votes)
3 Oct 2011CPOL 100.7K  
Validate numeric textbox using int.tryparse visual C#.NET
Hi there, I would like to post this simple example of tryparse method to validate a textbox. We use two textboxes and one button.

The idea is to enter a value in textbox1 and show the validation result on textbox2 when the event is fired, a message will appear in the end which will depend on whether we entered a number or alphabet. (I haven't gone as far as validating symbol yet).

I apologize before hand if it turns out there is a better example somewhere in this great forum.

Here's how I wrote the syntax.
C#
private void button1_Click(object sender, EventArgs e)
{
    int number2;
    if (int.TryParse(textBox1.Text, out number2))
    {
        textBox2.Text = ("tryparse method succeed");
    }
    else { textBox2.Text=("value entered is not numeric"); }
}


It's simple and easy, and it just happened its written out there in different examples, I thought I re-post it here, with a little modification.

God bless whoever invented the initial tryparse and those who bother writing down the many examples of tryparse.

License

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