Numbers-Only Textbox

vckicks 1 Tallied Votes 797 Views Share

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;
farooqaaa 48 Posting Whiz in Training

Thanks. Good one. It helped me.

nika0201 0 Newbie Poster

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

tmc01 0 Newbie Poster

doesnt work with back space

papanyquiL 45 Junior Poster

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

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


ddanbe 2,724 Professional Procrastinator Featured Poster

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

papanyquiL 45 Junior Poster

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.

ddanbe 2,724 Professional Procrastinator Featured Poster

Yes! This works fine!

Ravenheart 1 Light Poster

Use NumericUpDown control.

ai_enma 0 Newbie Poster

how about numeric textbox for

ddanbe 2,724 Professional Procrastinator Featured Poster

?? 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 forum.

debasishsahu4u 0 Newbie Poster

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;
islame-g 0 Newbie Poster

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

What does "\\d+" Means ??

ddanbe 2,724 Professional Procrastinator Featured Poster

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:

islame-g 0 Newbie Poster

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

tharakauka 0 Newbie Poster

Awsome man.. thanxxxx...

Member Avatar

wow, wonderful areas to read . thanks for every ideas given

Chatthanz 0 Newbie Poster
Note : In the keypress event of textbox type the below code

if(((int)e.KeyChar >=48 && (int)e.KeyChar <=57)||(int)e.KeyChar==08)




//Please note.The Ascii 08 is for allowing Back Space
//by Chathanz.. B-)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.