How can I prevent ListBox from unselecting items after right click

masoud_sedighy

Registered User.
Local time
Today, 04:47
Joined
Dec 10, 2011
Messages
132
i have a form that there is a list box inside that. after selection of items (usually 20 items) and right click the mouse on items it should open another pop up form,the problem is after right click selected items will be unselected except one item that there is mouse on that.
how can i prevent list box from deselecting items after right click .
the code for mouse right click is like below:


Private Sub ItemList_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Const RIGHTBUTTON = 2
Dim udtPos As POINTAPI
Dim frm As Access.Form

If Button = RIGHTBUTTON Then

Set mp = New [*clsMousePosition]
GetCursorPos udtPos


DoCmd.OpenForm "frmshortcut"
DoCmd.MoveSize udtPos.x * mp.TwipsPerPixelX, udtPos.y * mp.TwipsPerPixelY
Forms!frmshortcut!txtparameter = Me.ItemList.Value


End If

End Sub
 
Last edited:
A solution can be:
Code:
Option Compare Database
Option Explicit

Dim ItemsStatus As String

Private Sub ItemList_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
Dim i As Long
    If Button = RIGHTBUTTON Then
        ItemsStatus = ""
        For i = 0 To ItemList.ListCount - 1
            ItemsStatus = ItemsStatus & "," & ItemList.Selected(i)
        Next i
        ItemsStatus = Mid(ItemsStatus, 2)
    End If

End Sub

Private Sub ItemList_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Const RIGHTBUTTON = 2
Dim i As Long, SplitItemsStatus As Variant
    If Button = RIGHTBUTTON Then
        SplitItemsStatus = Split(ItemsStatus, ",")
        For i = 0 To ItemList.ListCount - 1
            ItemList.Selected(i) = SplitItemsStatus(i)
        Next i
    End If
 
End Sub

You must move the rest of the code under the MouseUp event, after the selected items are restored.
 
thanks, just a small problem, after right click on selected items ,that item has focus of mouse will be unhighlighted, that item also is in selected items and actually there is no problem, but visually it will be unhighlighted after right click and opened pop up form.
 
Not sure that this will work, but try to move the focus to other object in the same form before you open the popup form:
OtherObjectName.SetFocus
 

Users who are viewing this thread

Back
Top Bottom