Validating TextBox to Accept only numeric values

In This you will see that how to validate an textbox to accpet only Numeric value not any other values like char value except decimal value.so it accept only decimal and numeric value...

lets take an example that:
first place an TextBox on windows form then on KeyDown event of textbox write code that is written or shown below:


//Name of textbox is textBox1 and event is KeyDown







private
void textBox1_KeyDown(object sender, KeyEventArgs e)
{









   if (!((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) ||
((e.KeyCode >=
Keys.D0 && e.KeyCode <= Keys.D9) && (!e.Shift)) ||
e.KeyCode ==
Keys.Back || e.KeyCode == Keys.Delete || e.KeyCode ==
Keys.Alt || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right ||
e.KeyCode ==
Keys.Shift || e.KeyCode == Keys.Home || e.KeyCode ==
Keys.End || e.KeyCode == Keys.Decimal))

   
e.SuppressKeyPress =
true;
}
on above code e.KeyCode>=Keys.NumPad0 checking that value in textbox is
greter than 0 or not and same like that NumPad9 cheking
like that Keys.Back cheking for Backspace is pressed or not and
like that shift key pressed or not and Decimal is pressed or
not same like that checking other keys pressed or not and which
you can press or you able to press

//
//


after IF statement and code is written that is e.SuppressKeyPress=true;
here e is object of key event handler and SuppressKeyPress is bool
type accept true/false value and indicate that key event should
be passed on underlying control.


True if the key event should not be sent to the control; otherwise,
false.

we assign true to  SuppressKeyPress property in an event handler such as KeyDown
in order to prevent user input

Thanking you