Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-03
2
Medium Priority
?
726 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:Michael Sterling
[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 2000 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:Michael Sterling
ID: 39975278
Thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

704 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