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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Aug 1, 2013, 12:54 AM EDT
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8)
{
e.Handled = true;
}
}
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Jul 31, 2013, 11:44 PM EDT
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;
}
Ish BandhuRank #1,415 · 1.2K reputationPosted Jul 31, 2013, 11:55 PM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 1, 2013, 12:11 AM EDT
Use MaskedTest box and set Mask format as 000 (allow 3 digit)
Regards,
Iftikar
Pavan RamamurthyRank #1,682 · 906 reputationPosted Sep 13, 2013, 1:39 AM EDT
{
if (housenumber< 1)
{
messagebox.show("enter valid number") ;
}
}