Highlighting item on userform listbox without selecting it.

Herakles

Well-known Member
Joined
Jul 5, 2020
Messages
927
Office Version
  1. 365
Platform
  1. Windows
Hi All.

Using the vertical position of the mouse pointer when hovering over a listbox and then identifying which item is selected I want to
highlight the listbox item without selecting it.

Subsequently, the user may want to click on it to invoke the click event.

Is this possible?

Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Here is an example but don't use the Click event as it will iterfere. Instead, use the MouseDown event as follows:

UserForm Module:
VBA Code:
Option Explicit

Private Type POINTAPI
    X As Long
    Y As Long
End Type

#If VBA7 Then
    Private Declare PtrSafe Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long
#Else
    Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long
#End If

Private Sub UserForm_Initialize()
    Dim i As Long
    For i = 1& To 100&
        ListBox1.AddItem "Option " & i
    Next i
End Sub

Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    MsgBox "You Clicked Item: '" & ListBox1.Value & "'"
End Sub

Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim tXYPos As POINTAPI, oiA As IAccessible, vKid As Variant
    If Button = 0& Then
        Set oiA = ListBox1
        Call GetCursorPos(tXYPos)
        vKid = oiA.accHitTest(tXYPos.X, tXYPos.Y)
        If VarType(vKid) <> vbEmpty Then
            ListBox1.Selected(CLng(vKid) - 1&) = True
        End If
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top