Click here to Skip to main content
13,258,479 members (54,419 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 9 Sep 2011

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

, 3 Oct 2011
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.


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


About the Author

Aria Wijaya
Australia Australia
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...


Comments and Discussions

QuestionPlease explain Pin
atjoshi30-Jul-12 21:59
memberatjoshi30-Jul-12 21:59 
GeneralReason for my vote of 3 Validation should be performed first... Pin
DrABELL13-Sep-11 4:33
memberDrABELL13-Sep-11 4:33 
GeneralIf you are using int.tryParse to validate the value you may ... Pin
Reiss13-Sep-11 2:23
memberReiss13-Sep-11 2:23 
GeneralRe: That make sense, thank you. Feel free to edit the material a... Pin
Aria Wijaya13-Sep-11 2:42
memberAria Wijaya13-Sep-11 2: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by Aria Wijaya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid