Trapping Delete Key


Well-known member
Apr 6, 2005
Programming Experience
In keypress event I already put:

e.handled = true

It works fine. But if users press the Delete key, it still does not trap the key. How do I prevent this from happening?

Thank you.
here's one solutions (probably not the best) but in the KeyDown event put:

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
	If e.KeyCode = Keys.Delete Then e.Handled = True
End Sub
tried and it worked. Thank you. :)

Previously, I put it in the KeyPress. Hmm... just wondering the differences between KeyPress and KeyDown.
i'm not sure why in the keypress event if you set e.Handled = True the delete is still accepted perhaps it's a minor bug, but in the KeyDown event you can suppress the Delete key and then in the KeyPress event you can supress all the other keys

but it also occured to me that if you want all the keys supressed set the Textbox's ReadOnly property to True then change the BackColour and ForeColour to the Black text on White background

oh and the differences between keydown, keyup, and keypress is that keydown fires, then keypress fires and finally keyup fires that and e has different arguements in the different subs

ie in the keydown/keyup events e holds information like shift = true/false and control = true/false meaning you can check to see if the shift was also pressed producing a capital letter or not

the keypress event tells you which letter/number key was pressed and doesnt pay attention to the control, alt, spacebar, shift keys
Last edited: