How do I get a TextBox to only accept numeric input in WPF?

后端 未结 30 2991
悲哀的现实
悲哀的现实 2020-11-22 03:40

I\'m looking to accept digits and the decimal point, but no sign.

I\'ve looked at samples using the NumericUpDown control for Windows Forms, and this sample of a Num

30条回答
  •  独厮守ぢ
    2020-11-22 04:07

    I allowed numeric keypad numbers and backspace:

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            int key = (int)e.Key;
    
            e.Handled = !(key >= 34 && key <= 43 || 
                          key >= 74 && key <= 83 || 
                          key == 2);
        }
    

提交回复
热议问题