Thursday, December 15, 2011

How to Restrict Non-Numeric Key Press in Text Field

Sometimes you may need to do code in such way that the user may not enter dot[.] or any alphabetic character. Only Integer value should be entered into TextField. In such case you may capture the Key pressed by the User and allow only Numeric values.

The following code only takes numeric value without decimal point for textBox1

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);

No comments:

Post a Comment