• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1640
  • Last Modified:

C# Have text box only accept Numbers and Paste

I would like to make a text box in C# only accept numbers, backspace and pasted numbers here is the code i have used attached.   it handles backspace and numbers only but i am having a hard time getting it to accept paste.  Can anyone help on this?
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            //if (!Char.IsDigit(e.KeyChar)||!Char.IsControl(e.KeyChar))
            if ((!Char.IsDigit(e.KeyChar)) && (e.KeyChar != Convert.ToChar(Keys.Back)))
                e.Handled = true;

Open in new window

Andrew Bass
Andrew Bass
1 Solution
Have you considered using a MaskedTextBox?  This may be better suited to what you are doing.
Andrew BassTechnical SupportAuthor Commented:
I can not believe I have ignored the obvious.  Thanks For the quick response
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now