这通常是命中测试完成其列表框不支持,这里是一个哈克的方式;
添加另一个列表框称为lbTest
某处的形式,双击其BorderStyle
财产,直到它看起来像一个空的白色框,设置其visible
到false
Private LBI_HEIGHT As Long
Private Sub UserForm_Initialize()
'get the height of a single list item in twips based on the fact the box will resize itself automatically;
With lbTest
.Width = 100
.Height = 1
.AddItem "X"
LBI_HEIGHT = .Height
End With
'add test data
Dim i As Long
For i = 1 To 50
ListBox1.AddItem "item " & i
Next
End Sub
Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'get the item at the Y coord based on the scroll position & item height
Dim derivedIndex As Long
derivedIndex = (Y \ LBI_HEIGHT) + ListBox1.TopIndex
Me.Caption = derivedIndex & " = " & ListBox1.List(derivedIndex)
End Sub
哇,这太酷了。非常感谢您提供快速而有效的答案。一个简短的测试显示了准确的结果。 – phippu 2012-02-01 16:08:35