Number Only Textbox

// number only textbox using TryParse
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    int value = 0;
    e.Handled = !int.TryParse(e.KeyChar.ToString(), out value);
}

// number only textbox using Regex.IsMatch
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), “\\d+”))
        e.Handled = true;
}

Leave a Reply

Your email address will not be published. Required fields are marked *