Click to See Complete Forum and Search --> : Restricting the Key press in text box


vivekshah
June 22nd, 2006, 03:04 AM
Hello

I have an textbox that is always filled with numeric value.

i want to restrict other all the key press except the numeric key & - .
as for the phone the hyphen is required.

How to implement it?

I tried but no success.

Any Idea?

jhammer
June 22nd, 2006, 03:59 AM
Use the KeyPressed event of the TextBox:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = !(Char.IsDigit(e.KeyChar) || e.KeyChar == '-' || e.KeyChar == (char)8);
}

char #8 is Delete key.
setting the e.Handled to true prevents the key from showing.

vivekshah
June 22nd, 2006, 04:53 AM
Thanx for reply

But i have WEB APPLICATION ?(asp.net +C#)

So How do i do then?

Shuja Ali
June 22nd, 2006, 05:17 AM
You should avoid cross-posting the same question. Take a look at my reply to your other thread in ASP.NET forum
http://www.codeguru.com/forum/showthread.php?p=1413182#post1413182