click on item in listbox without selecting it.

zangyef

New Member
Joined
Mar 20, 2013
Messages
5
Hi everyone,
I have a listbox with multiselection active. I know that the click event is disable when multiselection is active but I want to find a way to run code when I click on item even if it is selected or not. I found no solution surfing the net. Is there a simple way to do it?
Thanks to all!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this:

VBA Code:
Private Sub ListBox1_Change()
   'Your code
End Sub
 
Upvote 0
I would use the MouseDown event.

Here is an adaptation of some code I have written in the past . The code uses a class module to sink Listbox MouseDown events and gives the user much more info than the standard Click event ... Info such as the listbox object, the item that was clicked , Shift key state, X & Y mouse coordinates .

Works with single as well as multiselect listboxes.

Workbook example


1- Code in the Class Module: (clsMultiSelectLBClickEvent)
VBA Code:
Option Explicit

Private WithEvents LB As MSForms.ListBox

Private Type POINTAPI
    X As Long
    Y As Long
End Type

#If VBA7 Then

    Private Declare PtrSafe Function DispCallFunc Lib "OleAut32.dll" ( _
        ByVal pvInstance As LongPtr, _
        ByVal oVft As LongPtr, _
        ByVal cc As Long, _
        ByVal vtReturn As Integer, _
        ByVal cActuals As Long, _
        ByVal prgvt As LongPtr, _
        ByVal prgpvarg As LongPtr, _
        ByVal pvargResult As LongPtr _
        ) As Long

    Private Declare PtrSafe Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long

    Private lEventProcAddr As LongPtr
#Else

    Private Declare Function DispCallFunc Lib "OleAut32.dll" ( _
        ByVal pvInstance As Long, _
        ByVal oVft As Long, _
        ByVal cc As Long, _
        ByVal vtReturn As Integer, _
        ByVal cActuals As Long, _
        ByVal prgvt As Long, _
        ByVal prgpvarg As Long, _
        ByVal pvargResult As Long _
        ) As Long

    Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long

    Private lEventProcAddr As Long
#End If


Private Sub LB_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    Const CC_STDCALL = 4&
   
    Dim tCurPos As POINTAPI, oiA As IAccessible, vKid As Variant
    Dim vArgArray(5) As Variant, varTypes() As Integer, result As Variant, i As Long
   
    #If Win64 Then
        Dim varPointers() As LongLong
    #Else
        Dim varPointers() As Long
    #End If

    If Button = 1 Then
        GetCursorPos tCurPos
        Set oiA = LB
        vKid = oiA.accHitTest(tCurPos.X, tCurPos.Y)
        Set vArgArray(0) = CVar(LB)
        vArgArray(1) = CVar(vKid - 1)
        vArgArray(2) = CVar(Shift)
        vArgArray(3) = CVar(X)
        vArgArray(4) = CVar(Y)
       
        For i = 0 To 4
            ReDim Preserve varTypes(i)
            ReDim Preserve varPointers(i)
            varTypes(i) = VarType(vArgArray(i))
            varPointers(i) = VarPtr(vArgArray(i))
        Next i
       
        Call DispCallFunc( _
            0, _
            lEventProcAddr, _
            CC_STDCALL, _
            VbVarType.vbDouble, _
            5, _
            VarPtr(varTypes(0)), _
            VarPtr(varPointers(0)), _
            VarPtr(result))
    End If

End Sub

#If Win64 Then
    Public Sub Attatch(ByVal ListBox As MSForms.ListBox, ByVal ClickEventProcAddr As LongLong)
#Else
    Public Sub Attatch(ByVal ListBox As MSForms.ListBox, ByVal ClickEventProcAddr As Long)
#End If

        Set LB = ListBox
        lEventProcAddr = ClickEventProcAddr
       
End Sub



2- Usage example ( the Callback 'ListBox_Click' event procedure must reside in a Standard Module because of the AddressOf statement)
VBA Code:
Option Explicit

Dim oListBox1 As clsMultiSelectLBClickEvent
Dim oListBox2 As clsMultiSelectLBClickEvent


Sub Example()

    'Enable Click events for ListBox1 and ListBox2
    Set oListBox1 = New clsMultiSelectLBClickEvent
    Set oListBox2 = New clsMultiSelectLBClickEvent
   
    oListBox1.Attatch Sheet1.ListBox1, AddressOf ListBox_Click
    oListBox2.Attatch Sheet1.ListBox2, AddressOf ListBox_Click

End Sub


'Gereric ListBox Click event
Sub ListBox_Click( _
        ByVal ListBox As MSForms.ListBox, _
        ByVal ItemClicked As Integer, _
        ByVal Shift As Integer, _
        ByVal X As Single, _
        ByVal Y As Single)
       
    MsgBox "Listbox name: '" & ListBox.Name & "'" & vbCr & _
    "Index of Item clicked: '" & ItemClicked & "'" & vbCr & _
    "Value of Item clicked: '" & ListBox.List(ItemClicked) & "'" & vbCr & _
    "X coord: '" & X & "'" & vbCr & _
    "Y coord: '" & Y & "'"

End Sub
 
Upvote 0
Try this:

VBA Code:
Private Sub ListBox1_Change()
   'Your code
End Sub

Thanks DanteAmor,
I already have that event code and initially I thought that I could work on it but I dont find a way to identify the item clicked. I mean, the code lisbox.selected(i) identify the item selected, not the item I clicked on.
 
Upvote 0
I would use the MouseDown event.

Here is an adaptation of some code I have written in the past . The code uses a class module to sink Listbox MouseDown events and gives the user much more info than the standard Click event ... Info such as the listbox object, the item that was clicked , Shift key state, X & Y mouse coordinates .

Works with single as well as multiselect listboxes.

Workbook example

Thank you very much Jaafar,
It is exactly what I need!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,816
Members
449,095
Latest member
m_smith_solihull

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