954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Numbers-Only Textbox

By vckicks on Jun 16th, 2008 12:01 pm

Use regular expressions, regex, to create an efficient textbox that only takes in digits as input.

//Add to the textbox's KeyPress event
private void txtBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
e.Handled = true;
}

Thanks. Good one. It helped me.

farooqaaa
Posting Whiz in Training
295 posts since Jul 2008
Reputation Points: 61
Solved Threads: 71
 

this works for integer values, but not for negative and noninteger
numbers

nika0201
Newbie Poster
1 post since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

doesnt work with back space

tmc01
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

for controls like backspace, del, etc... use this:

if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) e.Handled = true;


:)

papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
 

Your code does not work papanyquiL, at least on my system that is.
Check out this

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

@ddanbe
Maybe you're not using the same event handler that I am.. Here's the complete code

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //makes sure that textBox1 is a number
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsPunctuation(e.KeyChar)) e.Handled = true;
        }

It works perfectly on my end.

papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
 

Yes! This works fine!

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Use NumericUpDown control.

Ravenheart
Light Poster
27 posts since Nov 2009
Reputation Points: 11
Solved Threads: 5
 

how about numeric textbox for vb.net

ai_enma
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

@ai_enma
?? This snippet almost reads as vb. Use Sub End Sub instead of curly braces, and an If End If.
If that not helps you, ask in the vb.net forum.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Simplest method to use a numeric textbox which will accept the 'BackSpace' key also...

int isNum = 0;

if (e.KeyChar == '\b')
     e.Handled = false;
else if (!int.TryParse(e.KeyChar.ToString(), out isNum))
     e.Handled = true;
debasishsahu4u
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Please Help !!
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))

What does "\\d+" Means ??

islame-g
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Hi,islame-g, welcome here.
Please don't ressurrect old threads, read the rules!
You better start a new thread instead.
To learn about regular expressions, you could start here: http://www.regular-expressions.info/dotnet.html

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Hi ddnabe,So sorry ,I didn't know about this rule .
Many Thx. for your reply ,I think this link is very useful

islame-g
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: