C#: How to delete item from listbox using keyboard 'delete' button?

Hello,

 I would like to know how to delete an item from a listbox using the keyboard. For example, a listbox lives on a form and has some items in it. The user selects an item (single select mode). The user then types "delete" (a key press) on the keyboard. The item is then deleted from the listbox.


Thanks!
Tom
embalismAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag AgarwalPython DeveloperCommented:
Hi you can use either keypress or keyup events of listbox for capturing the keyboard events

private void listbox_keyUp(object sender, KeyUpEventArgs e)
 {

       if (e.KeyCode == Keys.Delete)
      {
          //remove selected item from list
      }
 }

Anurag
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
embalismAuthor Commented:
Thanks, that did the trick!

I created a static class to handle the removal (to help anyone else who has this issue). See code attachment. It also supports MoveUp, MoveDown, and Delete for any of your domain lists.



        public static void MoveUp(ListBox listBox, IList items)
        {
            if (listBox.SelectedIndex < 0)
                return;
 
            if (listBox.SelectedIndex == 0 || listBox.Items.Count < 2)
                return;
 
            var tempItem = items[listBox.SelectedIndex - 1];
            items[listBox.SelectedIndex - 1] = items[listBox.SelectedIndex];
            items[listBox.SelectedIndex] = tempItem;
 
            show(listBox, items);
        }
 
        public static void MoveDown(ListBox listBox, IList items)
        {
            if (listBox.SelectedIndex < 0)
                return;
 
            if (listBox.SelectedIndex == listBox.Items.Count - 1 ||
                listBox.Items.Count < 2)
                return;
 
            var tempItem = items[listBox.SelectedIndex + 1];
            items[listBox.SelectedIndex + 1] = items[listBox.SelectedIndex];
            items[listBox.SelectedIndex] = tempItem;
 
            show(listBox, items);
        }
 
        public static void Delete(ListBox listBox, IList items)
        {
            if (listBox.SelectedIndex > -1)
                items.RemoveAt(listBox.SelectedIndex);
            else
                return;
 
            show(listBox, items);
        }
 
        private static void show(ListBox listBox, IList items)
        {
            listBox.Items.Clear();
            foreach (var item in items)
                listBox.Items.Add(item);
        }
    }

Open in new window

0
embalismAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.