C# Have text box only accept Numbers and Paste

Posted on 2008-11-06
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

Question by:Andrew Bass
    LVL 4

    Accepted Solution

    Have you considered using a MaskedTextBox?  This may be better suited to what you are doing.
    LVL 1

    Author Closing Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now