Reputation: 12210
I'm trying to detect when the user presses certain keys in a text box and then I want to increase or decrease the value in the text according to their keypress and cancel out the key they pressed. The cancel part isn't working. I'm accustomed to just putting in KeyCode = 0 if I want to cancel their keypress, like I do in MS Access. However, this isn't working for me in VB6. The plus sign gets into the textbox.
Any suggestions?
Upvotes: 4
Views: 6953
Reputation: 1
Private Sub Text1_KeyUp(KeyCode As MSForms.ReturnInteger, Shift As Integer)
If KeyCode = 27 Then
'CONDITION
End If
End Sub
Upvotes: -1
Reputation: 75296
I'd say ignore the KeyDown
event and just use the KeyPress
event:
Private Sub RTB_KeyPress(KeyAscii As Integer)
' "Cancel" the keystroke
KeyAscii = 0
End Sub
The best way to do what you're doing, though, would be to set your form's KeyPreview
property to True
, and then add a handler for the form's KeyPress
event - this means the form gets a chance to handle any keystrokes first. By handling it here and cancelling as above, you can change the text in the textbox however you like.
I used Visual Basic for the first 10 years of my career, but I had to google for the above code. I recommend at least moving on to VB.NET - sometimes you can even import VB6 projects into .NET (sometimes).
Upvotes: 7