Click here to Skip to main content
15,885,127 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
3 Oct 2011CPOL 99.8K   5   5
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)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPlease explain Pin
atjoshi30-Jul-12 20:59
atjoshi30-Jul-12 20:59 
GeneralReason for my vote of 3 Validation should be performed first... Pin
DrABELL13-Sep-11 3:33
DrABELL13-Sep-11 3:33 
GeneralIf you are using int.tryParse to validate the value you may ... Pin
Reiss13-Sep-11 1:23
professionalReiss13-Sep-11 1:23 
GeneralRe: That make sense, thank you. Feel free to edit the material a... Pin
Aria Wijaya13-Sep-11 1:42
Aria Wijaya13-Sep-11 1:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.