列表框 mousedown 事件 returns 之前选择的项目

Listbox mousedown event returns previously selected item

我正在为多个列表框准备拖放解决方案。问题是,当我单击列表中的项目时,Windows(或任何人)将项目设置为蓝色,触发 mousedown 事件,但是 .Selected().Listindex mousedown 事件处理程序中处理的方法 return 先前选择的项目。通过再次单击(已经是蓝色的)项目,他们 return 正确地选择了该项目。

我在按住鼠标按钮的同时单击一个项目,该项目变成蓝色,我将它拖到另一个列表框,之前选择的项目就出现了。

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

    Dim MyDataObject As DataObject
    Dim sSelected As String
    Dim i As Long
    Dim Effect As Integer

    If Button = 1 Then
        sSelected = vbNullString

变量#1

        For i = 0 To pThisListBox.ListCount - 1
            If pThisListBox.Selected(i) Then
                sSelected = pThisListBox.List(i)
                Exit For
            End If
        Next

第 2 变量

        With pThisListBox
            If .ListIndex >= 0 Then
                sSelected = .List(.ListIndex)
            End If
        End With

普通

        If LenB(sSelected) = 0 Then Exit Sub
        Set DragSource = pThisListBox
        Set MyDataObject = New DataObject
        MyDataObject.SetText sSelected
        Effect = MyDataObject.StartDrag
        Debug.Print sSelected
    End If

End Sub

我已经试过 .MultiSelect,但没有用。它实际上设置为 fmMultiSelectSingle,但理想情况下是 fmMultiSelectMulti。 我是不是误会了什么?

试试这个(也适用于多选)

Private Sub pThisListBox_MouseMove(ByVal Button As _
 Integer, ByVal Shift As Integer, ByVal X As _
 Single, ByVal Y As Single)
 Dim MyDataObject As DataObject

 If Button = 1 Then
 Set MyDataObject = New DataObject
 Dim Effect As Integer
    For i = 0 To pThisListBox.ListCount - 1
            If pThisListBox.Selected(i) Then
                MyDataObject.Clear
                MyDataObject.SetText pThisListBox.List(i)
                Effect = MyDataObject.StartDrag
               End If
        Next
 End If
End Sub

简单的解决方案是正确的。

如果mouse_down的活动时间太早,就去mouse_up的活动。

我试过了,很管用。这是一个代码示例:

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

Dim Bonus$, i&

i = Listbox.ListIndex
Bonus = Listbox.List(i, 0)

If Button = 1 Then      
      Me.Caption = Bonus
End If

End Sub

为了不让它看起来很奇怪,在 mouse_down,我 me.caption="" .