Prevent a listview to change selected item when key pressed

Posted on 2009-02-24
Last Modified: 2012-05-06

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
Question by:cm2187
    LVL 10

    Accepted Solution

    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.
    LVL 4

    Expert Comment

    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.

    Author Comment


    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.



    Author Comment

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

    thanks for your help!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now