There a way to use e.Cancel () on a keyup event?
I’m trying to validate a textbox with a Regex and I need to cancel the event if it no meets the Regex expression, or delete the key pressed to meet so that the expression
For Example:
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
ERROR: ‘cancel’ is not a member of ‘System.Windows.Forms.KeyEventArgs’.
Try
From MSDN:
It’s unclear what you are trying to cancel though, since you are reading the TextBox.Text value.
KeyDownis usually the preferred event to intercept the keystroke.If you are trying to validate the entire string, the
Validatingevent might be more appropriate. With TextBox controls, you will always have the danger of someone pasting clipboard text into the control, which could bypass key events.