Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keypress event issues (PLEASE HELP.)

Status
Not open for further replies.

RotorTorque

Programmer
Apr 29, 2007
100
0
0
US
Hi all
I have written an app. in VB 2005. I have a textbox in my app. and I need to call a method whenever the BackSpace or Delete keys are pressed. I need to have this done from the txtTextBox_KeyPress or txtTextBox_KeyUp events. How can I capture or know when either of the above keys have been pressed?

Thanks in advance,
RotorTorque :)
 
Hello,

You need to check e.KeyCode in the KeyUp or KeyDown event, presumably KeyUp so you can trap it sooner. Not sure whether or not you need to set e.Handled to True. Check the Help or test to see which value you need. Good Luck!
Code:
Private Sub txtTextBox_KeyUp(ByVal sender As Object, _
                             ByVal e As System.Windows.Forms.KeyEventArgs) _
                                 Handles txtTextBox.KeyUp
    If e.KeyCode = Keys.Delete Then
        MsgBox("Delete key pressed")
        ' YourMethod(methodParms)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Back Then
        MsgBox("BackSpace key pressed")
        ' YourMethod(methodParms)
        e.Handled = True
    End If
End Sub 'txtTextBox_KeyUp
 
Sorry, I meant to say KeyDown instead of KeyUp above. If you use KeyDown and set e.Handled to True for the Delete key, the delete does not happen. There doesn't appear to be a corresponding difference in behavior for the BackSpace key. Good Luck!
 
Hi -

I am attempting a similar action using the KeyDown event within a ListBox. However if I attempt to use this syntax, It will not find the KeyDown event?

Is there something I need to declare or reference in order to get KeyDown functionality to work within a ListBox?

Any help would be greatly appreciated!

Thanks,

-b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top