我真的需要阻止任何非数字输入(包括粘贴)到我的TextBox,并且我想要防止它在被删除之前出现在TextBox上,如果你对你的代码建议提出一些提示,那将是很棒的,因为我只是一个初学者!
答案 0 :(得分:0)
您可以使用此代码,以防止处理数值:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Then ' Check if the value is a number
e.KeyChar = ChrW(0)
e.Handled = True
End If
End Sub
并且对于 Ctrl V 处理,您可以使用以下代码:
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
' Checking if control + v is clicked or Shift + Insert is clicked
If (e.Control AndAlso Char.ToLower(ChrW(e.KeyValue)) = "v"c) Or (e.Shift AndAlso e.KeyValue = Keys.Insert) Then
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
答案 1 :(得分:-1)