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;
}

// NumberTextBoxLib custom control (inherited from TextBox)
public
partial class NumberTextBox : TextBox
{
public NumberTextBox()
{
InitializeComponent();
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
int value = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out value);

}
}

1305433735.zip

Leave a Reply

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