Click here to Skip to main content
11,703,632 members (63,314 online)
Click here to Skip to main content

Tagged as

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

, 2 Oct 2011 CPOL 4.5K 2
Rate this:
Please Sign up or sign in to vote.
You could always run the TryParse on the keyDown event so as to validate as the data gets entered. It saves the user an additional UI interaction. private void textBox1_KeyDown(object sender, KeyEventArgs e) { int i; string s = string.Empty; ...
You could always run the TryParse on the keyDown event so as to validate as the data gets entered. It saves the user an additional UI interaction.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            int i;
 
            string s = string.Empty;
 
            s += (char)e.KeyValue;
 
             if (!(int.TryParse(s, out i)))
            {
                e.SuppressKeyPress = true;
                textBox2.Text = "An int, that key press would not create";
            }
            else
            {
                textBox2.Text = "On the path to a great int, you are";
            }             
        }

License

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

Share

About the Author

endozs
Systems Engineer Stratco
Australia Australia
I'm an Aircraft Maintenance Engineer gone wild.
After leaving the Air Force I studied Business Info Systems at university and am now working as an automation expert.

Primarily on automation control systems, Factory SCADA, Human Machine interface.

Basically have free reign to improve /build new our manufacturing systems so the work is fun, a nice blend of software and hardware.

Admin for a handful of servers across the country, web master, db admin, maintenance, problem solver, magician, ninja.....

Think Jack of all trades master of none Smile | :)

You may also be interested in...

Comments and Discussions

 
GeneralRe: Ahh, of course (Slaps palm against forehead), rather foolish... Pin
endozs25-Sep-11 12:25
memberendozs25-Sep-11 12:25 
GeneralRe: Not quite. Two examples: 1) If the textbox contains 21474836... Pin
Luc Pattyn24-Sep-11 17:04
mvpLuc Pattyn24-Sep-11 17:04 
GeneralRe: thanks endozs Pin
Aria Wijaya24-Sep-11 16:46
memberAria Wijaya24-Sep-11 16:46 
GeneralThat is incorrect, your code is assuming the caret is at the... Pin
Luc Pattyn21-Sep-11 6:55
mvpLuc Pattyn21-Sep-11 6:55 
GeneralRe: Hi Luc, thanks for the feedback, although I don't necessaril... Pin
endozs21-Sep-11 11:24
memberendozs21-Sep-11 11:24 
GeneralRe: thank you for posting this Pin
Aria Wijaya24-Sep-11 16:45
memberAria Wijaya24-Sep-11 16:45 

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 | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by endozs
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid