Solved

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

Posted on 2014-04-03
2
685 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

680 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