take a look to the form:-
You can see the House Number TextBox. I want the code that this control only accept the numbers as input by the user. It does not accept any key other than numbers.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Aug 1, 2013, 4:54 AM
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8)
{
e.Handled = true;
}
}
Pavan RamamurthyPosted Sep 13, 2013, 5:39 AM
{
if (housenumber< 1)
{
messagebox.show("enter valid number") ;
}
}
Iftikar HussainPosted Aug 1, 2013, 4:11 AM
Use MaskedTest box and set Mask format as 000 (allow 3 digit)
Regards,
Iftikar
Ish BandhuPosted Aug 1, 2013, 3:55 AM
Sanjeeb LenkaPosted Aug 1, 2013, 3:44 AM
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
or you can use this
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string[] nums = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
if (!nums.Contains(e.KeyChar.ToString()))
e.Handled = true;
}