简体   繁体   English

VB.net:只允许数字,在WPF中复制粘贴回空格删除?

[英]VB.net: Only allow digits, copy paste back space delete in WPF?

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 ? . , - , 以及在 VB.net WPF的文本框中复制粘贴Ctrl C + Ctrl V, backspace and delete

As it is WPF, the keystrokes and keypresses do not work.由于它是 WPF,击键和按键不起作用。 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!请帮忙!

In cases like this, I tend to use the numericUpDown control from the VB Toolbox.在这种情况下,我倾向于使用 VB 工具箱中的 numericUpDown 控件。 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.此外,您可以设置最小值和最大值。

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`

this simple code will help you to put numeric only for textboxes on VB.net WPF这个简单的代码将帮助您将数字仅用于 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM