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.
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.
Munesh SharmaPosted Feb 3, 2015, 2:26 AM
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);
}
Santhakumar MunuswamyPosted Feb 2, 2015, 9:56 PM
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