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

后端 未结 30 2750
悲哀的现实
悲哀的现实 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:09

    Use:

    Private Sub DetailTextBox_PreviewTextInput( _
      ByVal sender As Object, _
      ByVal e As System.Windows.Input.TextCompositionEventArgs) _
      Handles DetailTextBox.PreviewTextInput
    
        If _IsANumber Then
            If Not Char.IsNumber(e.Text) Then
                e.Handled = True
            End If
        End If
    End Sub
    

提交回复
热议问题