Prevent a listview to change selected item when key pressed


I am using a listview in VB.Net. When a key is pressed, by default the listview will select the item which first letter matches the key pressed. I would like to disable that, in order to be able to assign keys to certain actions. I can't see any obvious way to do it.  Would anyone know how to do this?

Thanks in advance
Who is Participating?
lucius_theConnect With a Mentor Commented:
I am not sure you will be able to do this, but my idea is to try this:
Open event handlers for KeyPress, KeyDown and KeyUp events. In them set the "e.handled" property to True. This way you might tell the compiler that your event has been handled, amybe then it won't handle it itself.

Another option I have in mind, if the first one doesn't work, try to make a custom control. You custom control should inherit listview. Then override the KeyPress, KeyDown and KeyUp event. This way the original evets will not fire, and you might get what you want. Then use that control instead of the original listview.
If the above does not work; you could opt for saving the currently selected index into a variable, and then set the selecteditems index after a keypress event in order to put it back.
cm2187Author Commented:

thanks for your responses. I've tried setting e.handled=true, but that still triggers the selection of the item.  

Even saving the selection manually and putting it back doesn't seem to help as VB selects the item only after the keypress sub is executed.


cm2187Author Commented:
ooops. Correction. I was trying e.handled on keydown. But if I do it on keypress, then the solution works.

thanks for your help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.