Click to See Complete Forum and Search --> : help


wise programmer
August 31st, 2006, 03:29 PM
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.

Ejaz
August 31st, 2006, 03:38 PM
Take a look at How to: Create a Numeric Text Box (http://msdn2.microsoft.com/en-us/library/ms229644.aspx).

Furthermore, plz use meaningful titles to your thread. It will bring more audience to your problem.

thelinuxduck
August 31st, 2006, 03:50 PM
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.


this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);


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.

Here's an example:

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;
}
}
}

wise programmer
September 1st, 2006, 09:18 AM
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.

MadHatter
September 1st, 2006, 10:10 AM
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.