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

Dani AI

Generated

Short diagnosis and a safe pattern to use.

The call to PerformMouseClick() is not something the WinForms ListBox exposes — that approach will not compile and you should treat SelectedItem as the selected object rather than trying to assign a click result. Factoring the work (open/process item) into a single method that both your mouse/double‑click handler and your Enter handler call, as suggested by , is the correct design. (learn.microsoft.com)

Use the KeyDown event, check for Enter, suppress the system beep, then process the selected item(s). Example (VB.NET):

Private Sub ListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True ' stop the ding
        If ListBox1.SelectionMode = SelectionMode.One Then
            Dim sel = ListBox1.SelectedItem
            If sel IsNot Nothing Then ProcessSelected(sel)
        Else
            For Each idx As Integer In ListBox1.SelectedIndices
                ProcessSelected(ListBox1.Items(idx))
            Next
        End If
    End If
End Sub

Private Sub ProcessSelected(item As Object)
    ' centralize the action here (same method can be called from DoubleClick)
    MessageBox.Show(String.Format("Do action on: {0}", item.ToString()))
End Sub

Using KeyDown and SuppressKeyPress prevents the Enter keystroke from producing a beep or generating additional KeyPress/KeyUp behavior. For multi‑select lists iterate SelectedIndices; for single selection check SelectedItem or SelectedIndex. (learn.microsoft.com)

If you need Enter to work even when the ListBox does not have focus, set the form’s KeyPreview = True and handle the form key events (then ensure you only act when the ListBox is relevant). This is a reliable, maintainable pattern. (learn.microsoft.com)

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.