Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 3 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.
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)

Share

About the Author

Aria Wijaya

Australia Australia
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 3 Oct 2011
Article Copyright 2011 by Aria Wijaya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid