How to make a textbox which is only accept the numeric value in WPF

Total Post:28

Points:196
WPF 
WPF
 2417  View(s)
Ratings:
Rate this:

Hello Mindstick!

I need to make a textbox in wpf application which is accept only numeric value.

thanks in advance

  1. Post:105

    Points:735
    Re: How to make a textbox which is only accept the numeric value in WPF

    Hi !

    try this
    private void myTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                switch(e.Key)
                {
                    case Key.D0:
                    case Key.D1:
                    case Key.D2:
                    case Key.D3:
                    case Key.D4:
                    case Key.D5:
                    case Key.D6:
                    case Key.D7:
                    case Key.D8:
                    case Key.D9:
                    case Key.NumLock:
                    case Key.NumPad0:
                    case Key.NumPad1:
                    case Key.NumPad2:
                    case Key.NumPad3:
                    case Key.NumPad4:
                    case Key.NumPad5:
                    case Key.NumPad6:
                    case Key.NumPad7:
                    case Key.NumPad8:
                    case Key.NumPad9:
                    case Key.Back:
                        break;
                    default:      e.Handled = true;
                        break;
                }
            }

      Modified On Mar-30-2018 05:20:42 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!