List Box Delete Selected On Delete Key Press

GingerCode

New member
Joined
Aug 14, 2014
Messages
4
Programming Experience
Beginner
Hello, I have a listbox that I am trying to delete a selected item when I press the delete key. I have tried this on key down and key press, with no luck.

Any advice would be greatly appreciated.

Thank you


Private Sub lbTicketBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles lbTicketBox.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Delete) Then
lbTicketBox.Items.RemoveAt(lbTicketBox.SelectedIndex)
End If
End Sub
 
Hi,

Try this instead:-

Private Sub ListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
  If e.KeyCode = Keys.Delete AndAlso ListBox1.SelectedIndex > -1 Then
    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
  End If
End Sub


Hope that helps.

Cheers,

Ian
 
Excellent IanRyder! that worked great, I think when I attempted to use key press before I was still using e.keychar instead of your e.keycode.

Thank you very much!
 
Back
Top