在Word中右键单击选择列表框项目VBA
问题描述:
我正在使用VBA开发Word 2003中的项目。我有一个多选ListBox与一些条目(日期)。右键单击我想要弹出一个InputBox,用户可以在其中更改所选日期。这很有效,只要特定的项目已经集中(不仅被选中,而且集中)。但是,如果您右键单击某个没有焦点的项目,该框将显示并更改焦点条目的日期,而不是总是您右键单击的项目。在Word中右键单击选择列表框项目VBA
我找到了答案(http://www.vbarchiv.net/tipps/tipp_920-rechtsklick-in-der-standard-listbox-erkennen.html),但在VBA中是不可能的。有没有人为VBA提供解决方案?
我实际上需要在框出现之前在rightclick上更改关注的项目。
谢谢
答
这通常是命中测试完成其列表框不支持,这里是一个哈克的方式;
添加另一个列表框称为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