Special chracters
i want text box accept numaric and this sign - nothing els
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
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
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 20, 2013, 2:00 AM EDT
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
const char hyphen = (char)0x2D;
const char delete = (char)0x08;
if (!char.IsNumber(e.KeyChar) && e.KeyChar != hyphen && e.KeyChar != delete)
e.Handled = true;
}
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 20, 2013, 2:02 AM EDT
Are you looking for in ASP.NET or Windows Form?
Regards,
Iftikar
VulpesRank #55 · 29.2K reputationPosted Aug 20, 2013, 2:27 AM EDT
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
const char hyphen = (char)0x2D;
const char delete = (char)0x08;
if (!char.IsNumber(e.KeyChar) && e.KeyChar != hyphen && e.KeyChar != delete)
e.Handled = true;
if (e.KeyChar == hyphen && textBox1.Text.Length > 0)
e.Handled = true;
}