0

I'm trying to select items from the listbox when pressing Enter on the selected item. Here is what I have tried so far:

Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter
                ListBox1.SelectedItem = ListBox1.PerformMouseClick()
        End Select
    End Sub
2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by G_Waddell
0

Hi,
You want to select an item when the user selects the item and presses enter? But the user has just selected the item?
Looking at your code, it would appear you want the item to perform an action when the user hits enter and you may want other events to perform this action?
If this is the case, create a sub routine to perform the action(s) and in each of the sub routines that handle the events call it i.e.

Private Sub ListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListBox1.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter
                ListBox1_PerformMyAction()
        End Select
End Sub


 Private sub  ListBox1_PerformMyAction()
     if ListBox1.SelectedItems.Count = 0 then
         Messagebox.Show("No Item Selected")
     Else
        '.... do whatever
     end if
 End sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.