Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
grab and show facebook pages event calendars 6 44
Spacing between controls 4 16
Adjust the position 3 12
Open a word document 23 13
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

21 Experts available now in Live!

Get 1:1 Help Now