Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 KeyDown Fails to Cancel the Keypress

Tags:

vb6

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?

like image 303
HK1 Avatar asked Sep 19 '25 09:09

HK1


1 Answers

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).

like image 52
MusiGenesis Avatar answered Sep 23 '25 12:09

MusiGenesis