[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# Have text box only accept Numbers and Paste

Posted on 2008-11-06
2
Medium Priority
?
1,634 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:Andrew Bass
2 Comments
 
LVL 4

Accepted Solution

by:
BToson earned 2000 total points
ID: 22899530
Have you considered using a MaskedTextBox?  This may be better suited to what you are doing.
0
 
LVL 1

Author Closing Comment

by:Andrew Bass
ID: 31514110
I can not believe I have ignored the obvious.  Thanks For the quick response
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question