Capture left / Right click of listbox (1 Viewer)

Spooky

Registered User.
Local time
Today, 00:36
Joined
Jun 11, 2012
Messages
18
Is there a was to send a left click selection of a listbox to a form?

Using the code below, a user right clicks on listbox1 and is presented with a form to add/remove (and further options) the list box item. Making the selection in userform1 returns them to the main userform and the item is now selected. This works perfectly.

The code does work with left click, however on return to the form, the list item is not selected.

Code:
Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    
    Dim L As Long

    If Button = 2 OR Button =  1 Then
        L = ListBox1.TopIndex + Int(Y / (ListBox1.Font.Size * 1.2))
        ListBox1.ListIndex = L
        Userform1.INDEX.Value = L
        Userform1.show
    End If
End Sub
Any ideas or tweaks?
Thanks
 

Ranman256

Well-known member
Local time
Today, 03:36
Joined
Apr 9, 2015
Messages
4,339
Use dbl-click and the delete key.
There are already events for it.
On key down
If key = vbKeyDelete then
docmd.openquery "qdDelListBoxItem"
LstBox.requery
End if
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 18:36
Joined
Jan 20, 2009
Messages
12,849
The code does work with left click, however on return to the form, the list item is not selected.

To reselect the item try something like:

Code:
With Me.Listboxname
     intX = .ListIndex
  
    ' do your stuff
  
    .Item(intX).Selected = True
 End With
 

Spooky

Registered User.
Local time
Today, 00:36
Joined
Jun 11, 2012
Messages
18
To reselect the item try something like:
Thats why I find it weird - it is reselected in Userform1.
The only difference is whether button 1 (left) or 2 (right) is pressed to get there
 

Users who are viewing this thread

Top Bottom