Saturday, April 13, 2013

Accept Only Numeric Textbox

Simple code to accept only numeric in Textbox. You have to handle the key press event of textbox and have to check the key character is numeric or not.

Code:

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