right click listbox

supmktg

Registered User.
Local time
Yesterday, 20:42
Joined
Mar 25, 2002
Messages
360
I'm trying to create a right-click event on a listbox that will copy selected listbox item(s) to a temp table. So far, I've got this code to acknowledge the right click:
Code:
Private Sub List0_MouseDown(Button As Integer, Shift As Integer, X As Single, 
Y As Single)
    If Button = acRightButton Then
        MsgBox "You pressed the right button."
    End If
End Sub
Problem is the selected item on the list box doesn't move until after the mouse down event so whatever code I would run would involve the wrong record(s).

I'm using Access 2000 and 2003. Can someone tell me how to get the the correct record selected on mouse down, or point me to a working example of right-click functionality on a listbox.

Thanks,
Sup
 
Handle MouseUp instead of MouseDown.
 
Mouseup has 2 issues that I've encountered so far:

1) with multiselect enabled and multiple selections made, mouseup deselects the "right-clicked" listbox item.

2) after mouseup is done, the MS copy/paste/properties menu pops up.
 
My suggestion is that you not use a right click to trigger this table-making process. Maybe use a button or something.

This sidesteps the popup menu and the list selection/deselection issues that arise from right-clicking the list.

Here's code that solves the problems you mention, but it's a cumbersome approach...
Code:
[SIZE="1"]Private m_sel As Variant

Private Sub List0_GotFocus()
    Me.ShortcutMenu = False
End Sub

Private Sub List0_LostFocus()
    Me.ShortcutMenu = True
End Sub

Private Sub List0_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As Integer
    
    If Button = vbKeyRButton Then
        'create array of what is selected in the list
        ReDim m_sel(Me.List0.ListCount - 1)
        For i = 0 To Me.List0.ListCount - 1
            m_sel(i) = Me.List0.Selected(i)
        Next
    End If

End Sub

Private Sub List0_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim i As Integer
    
    If Button = vbKeyRButton Then
        'use previously created array to undo the click
        For i = 0 To Me.List0.ListCount - 1
            Me.List0.Selected(i) = m_sel(i)
        Next
        MsgBox "Run right click triggered process here"
    End If

End Sub
[/SIZE]
 
Lagbolt,

I wanted the user to have an option of using a right click shortcut or a button to move the records. The code you've shown me helps me do that.

I appreciate your help very much!
Sup
 

Users who are viewing this thread

Back
Top Bottom