Listbox selection with Right Click (using LB_ITEMFROMPOINT)
Allows selection of listbox items with right-click. *Not trying to get any votes, just sharing help I've provided in VB Discussion forum to everyone. Enjoy.*
AI
Resumo por IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Código fonte
Private Sub Form_Load()
Dim i As Integer
'Fill the listbox
For i = 1 To 5
List1.AddItem "Item " & i
Next
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lRet As Long
Dim lXPos As Long, lYPos As Long
'Convert the cursor position into pixels, because that is what is needed
lXPos = CLng(X / Screen.TwipsPerPixelX)
lYPos = CLng(Y / Screen.TwipsPerPixelY)
'If the right mouse button is clicked...
If Button = 2 Then
'Get the listitem closest to the cursor
'NOTE: Since the X and Y values have to be in the form of high and low
'order words, send the values as ((lYPos * 65536) + lXPos)
lRet = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0, ByVal _
((lYPos * 65536) + lXPos))
'If the returned value is a valid index, then set that item as the selected
'item
If lRet < List1.ListCount Then
List1.ListIndex = lRet
End If
End If
End Sub
Comentários originais (3)
Recuperado do Wayback Machine