Hi,
I read many articles about this, but I still couldn't manage to solve it. I want to select the item of a listbox with MouseEnter event.

It should be something similar like this

Private Sub LBox_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles LBox.MouseDown
        If e.Button = MouseButtons.Right Then
            Dim index As Integer = LBox.IndexFromPoint(New Point(e.X, e.Y))
            If index >= 0 Then
                LBox.SelectedItem = LBox.Items(index)
                Debug.Print(LBox.SelectedItem)
            End If
        End If
    End Sub

Would appreciate your help.
Thank you.

Dani AI

Generated

Quick clarification for : MouseEnter in WinForms raises with plain EventArgs, so there are no X/Y properties. If you really want to select the item when the cursor first enters the listbox, compute the mouse location yourself and translate it to client coordinates:

Private Sub LBox_MouseEnter(sender As Object, e As EventArgs) Handles LBox.MouseEnter
    Dim pt As Point = LBox.PointToClient(Control.MousePosition)
    Dim idx As Integer = LBox.IndexFromPoint(pt)
    If idx >= 0 Then LBox.SelectedIndex = idx
End Sub

If you want the selection to follow the cursor as it moves over items (what most people expect from hover-select), use MouseMove instead. Building on ’s direction, update the actual selection and avoid redundant assignments:

Private Sub LBox_MouseMove(sender As Object, e As MouseEventArgs) Handles LBox.MouseMove
    Dim idx As Integer = LBox.IndexFromPoint(e.Location)
    If idx >= 0 AndAlso idx <> LBox.SelectedIndex Then
        LBox.SelectedIndex = idx
    End If
End Sub

Notes:

  • Keep SelectionMode = One (default) so only one item is selected.
  • Option Strict On helps catch type mismatches like assigning an index to a String variable.

About the ComboBox follow-up: unlike ListBox, the drop-down portion of a WinForms ComboBox is a native list window that does not raise managed MouseMove, and there is no IndexFromPoint API. That is why saw it only working over the edit area. To hover-select in a combo you must either:

  • Subclass/owner-draw and hook the drop-down list via interop (advanced), or
  • Use a popup containing a ListBox (e.g., a ToolStripDropDown with a ListBox) and apply the MouseMove technique above.

If hover-to-select is a requirement, a ListBox-in-popup is the simplest maintainable route in WinForms.

Recommended Answers

All 8 Replies

Are you programming in WPF or Winforms?

Winforms

You can do it by

Private Sub ListBox1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove

    Dim G As System.Drawing.Graphics = Graphics.FromHwnd(Me.Handle)
    Dim item As String

    item = ListBox1.IndexFromPoint(New Point(e.X, e.Y))

    If item >= 0 Then
        TextBox1.Text = ListBox1.Items(item)
    End If

End Sub

It shows error message:

'X' is not a member of 'System.EventArgs'.  
'Y' is not a member of 'System.EventArgs'.

Thank you. I need to do with MouseEnter event.

Hi,

Thanks. I changed MouseEnter Event to MouseMove Event for all controls. Now, I am getting problem with ComboBox.

Could you please help me to do the same thing with comboBox?

Would appreciate your help

The MouseMove appears to operate only within the text area of the combobox and not when the dropdown is active. I don't have anything to suggest in this case but because the original question has been resolved please mark this thread as solved.

Thanks

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.