0

how do I only allow digits, no . , -, and also copy paste Ctrl C + Ctrl V, backspace and delete in a textbox in VB.net WPF?

As it is WPF, the keystrokes and keypresses do not work. I do not know the solution to this.

I cannot seem to find a solution for this, without this my program will break every time someone enters anything other than digits. Please help!

pleasega
  • 543
  • 1
  • 3
  • 21
  • possible duplicate of [How do I get a TextBox to only accept numeric input in WPF?](http://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf) – Rick Smith Jul 06 '15 at 18:19
  • Hi i would like it in vb.net as i do not know any c. Hope you understand. – pleasega Jul 06 '15 at 18:22
  • Ahh, missed that...sorry. Try here: http://stackoverflow.com/questions/4801430/only-accept-digits-for-textbox – Rick Smith Jul 06 '15 at 18:25
  • Hi i saw this too but it cant be used as mine is WPF and wpf does not have keychars. Which is why i emphasized on vb.net wpf. Thanks – pleasega Jul 06 '15 at 18:30

3 Answers3

0

In cases like this, I tend to use the numericUpDown control from the VB Toolbox. It only accepts numeric values and has some handy buttons to increase or decrease the value. Also, you can set a minimum and maximum value.

ThatGuy
  • 228
  • 1
  • 12
  • hi wpf doesnt have numericupdown and I cannot input `Controls:TextBoxHelper.ClearTextButton="True"` with a numeric updown. Any other ideas? – pleasega Jul 07 '15 at 05:50
  • You would have to install the wpf extended toolkit http://wpftoolkit.codeplex.com/ And use an integer, double or decimal up-down tool. – ThatGuy Jul 09 '15 at 03:27
0

Oh, i found the answer, just include this function.

Protected Overrides Sub OnPreviewTextInput(e As    
System.Windows.Input.TextCompositionEventArgs)
e.Handled = Not AreAllValidNumericChars(e.Text)
MyBase.OnPreviewTextInput(e)
End Sub


Private Function AreAllValidNumericChars(str As String) As Boolean
For Each c As Char In str
    If Not [Char].IsNumber(c) Then
        Return False
    End If
Next
Return True

End Function`
pleasega
  • 543
  • 1
  • 3
  • 21
0

this simple code will help you to put numeric only for textboxes on VB.net WPF

Private Sub txtTextBox_PreviewKeyDown(sender As Object, e As KeyRoutedEventArgs) Handles txtTextBox.PreviewKeyDown
    If e.Key <> 8 Then
        '48 - 57  = Ascii codes for numeric only
        If e.Key < 48 Or e.Key > 57 Then
            e.Handled = True
        End If
    End If
End Sub