有一种方法可以在 keyup 事件上使用 e.Cancel () 吗?
我正在尝试使用 Regex 验证文本框,如果事件不符合 Regex 表达式,我需要取消该事件,或者删除满足该表达式的按键
例如:
Dim rex As Regex = New Regex("^[0-9]{0,9}(\.[0-9]{0,2})?$")
Private Sub prices_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Textbox1.KeyUp,
Dim TxtB As TextBox = CType(sender, TextBox)
If (rex.IsMatch(TxtB.Text) = False ) Then
e.cancel = true
End If
End Sub
错误:“取消”不是“System.Windows.Forms.KeyEventArgs”的成员。
最佳答案
尝试
If (rex.IsMatch(TxtB.Text) = False) Then
e.SuppressKeyPress = True
End If
来自 MSDN :
You can assign true to this property in an event handler such as KeyDown in order to prevent user input.
Setting SuppressKeyPress to true also sets Handled to true.
虽然不清楚您要取消什么,因为您正在读取 TextBox.Text 值。 KeyDown
通常是拦截击键的首选事件。
如果您尝试验证整个字符串,Validating
事件可能更合适。使用 TextBox 控件时,您将始终面临有人将剪贴板文本粘贴到控件中的危险,这可能会绕过键事件。
关于regex - 取消 KeyUP 事件的处理程序?网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10558948/