Hi, I want to know how can I allow a TextBox to accept only numeric keys, I know how to do it in WinForms, but how can I do it in WPF ?
Please help asap.
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
Santhakumar MunuswamyRank #128 · 17.3K reputationPosted Feb 2, 2015, 4:56 PM EST
You can try below links it will be helpful
Winforms
http://jsfiddle.net/Lm2hS/
WPF
http://www.c-sharpcorner.com/Resources/675/
http://www.wpfsharp.com/tag/textbox/
I hope you will get an idea about this. I you got helpful this please mark accepted answer
Munesh SharmaRank #174 · 13.9K reputationPosted Feb 2, 2015, 9:26 PM EST
private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!char.IsDigit(e.Text, e.Text.Length - 1))
{
e.Handled = true;
}
}
for wf:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
{
if (!char.IsDigit(e.KeyChar) )
{ e.Handled = true;
} } }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !Char.IsDigit(e.KeyCode);
}