hi to all;
plz help me i want to restrict the text box control to take only digit as input. i m using vs.net 2003 (c#).
loking forward to u people reply.
TC.
Printable View
hi to all;
plz help me i want to restrict the text box control to take only digit as input. i m using vs.net 2003 (c#).
loking forward to u people reply.
TC.
Take a look at How to: Create a Numeric Text Box .
Furthermore, plz use meaningful titles to your thread. It will bring more audience to your problem.
Use the KeyDown event of the TextBox. It's important to use the KeyDown because it's EventHandler contains the SuppressKeyPress property which is how we ignore keys we don't want.
In the actual event handler, make sure that you allow not just digits, but others things like delete and backspace, so that the user can fix a typo. It would be wise to also allow arrows and shift keys to let them select their entry or move about in it. And, I'd suggest allowing the tab and enter keys.Code:this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
Here's an example:
Code:void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) {
if (e.KeyCode != Keys.Delete && e.KeyCode != Keys.Back) {
e.SuppressKeyPress = true;
}
}
}
when i m compiiling this code the error message is displayed:
" 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'SuppressKeyPress'".
I have added the key down event of text box.
thats because thats a 2.0 specific property.
feel free to use this one: http://www.sanity-free.org/article3.html capture key down only takes care of the use case where the user acutally types the text in... pasting will still allow text. the sample I posted manages that as well.