Solved

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

Posted on 2014-04-03
2
711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 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