Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have created a numeric textbox usercontrol in C#.net its working as i need but i m not able to delete values from that textbox using delete and backspace button
Posted

1 solution

I have no idea what your code looks like, anyway even I was stuck once while doing backspace. Here is a sample example of a code to allow only numeric values-

C#
protected override void OnKeyDown(KeyEventArgs e)
{
    //To only allow numbers
    if (char.IsNumber((char)e.KeyValue) || (((char)e.KeyData) == '\b'))
    {
       e.SuppressKeyPress = false;
    }
    else { e.SuppressKeyPress = true; }
    }


Have a look at the second part of the If condition - ((char)e.KeyData) == '\b')
This will allow you to use backspace in your textbox.

Hope this helps.

I have created one such control explained in my blog here - http://tarundotnet.wordpress.com/2011/06/26/allow-numbers-or-letters-and-disable-right-click-in-textbox/[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Jul-11 13:23pm    
A known problem: backspace is, by weird historical reason, a character code point, should be allowed explicitly as you do; a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900