Solved

How do I add key shortcut to my ListBox control to open selected items?

Posted on 2014-04-03
2
680 Views
Last Modified: 2014-04-03
I'm rewriting an application (converting from VB to C#). The application contains a list box, which contain file names. In the old VB application if a user clicked Ctrl+b while one of the the files in the list was highlighted it would open the file up in a text box. I'd rather just open the file up in a notepad, but I'm comfortable doin it either way. Just need to know how to add that functionality to the listbox item so that when its selected/highlighted and the user clicks Ctrl+b it will open the file.
0
Comment
Question by:mikesExpertExchange
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39975059
You can hook into the KeyDown event for the form and check the key combination being pressed:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    // check for Ctrl+B
    if (e.Control && e.KeyCode == Keys.B)
    {
        // check if an item is selected
        if (listBox1.SelectedIndex > -1)
        {
            MessageBox.Show(listBox1.SelectedItem.ToString());
        }
    }
}

Open in new window

You also need to make sure the KeyPreview property of the Form is set to True.
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 39975278
Thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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