我正在使用 VBA 在 Word 2003 中开发一个项目。我有一个带有一些条目(日期)的多选列表框。右键单击时,我希望弹出一个输入框,用户可以在其中更改所选日期。只要特定项目已经被聚焦(不仅被选择,而且被聚焦),这种方法就很有效。但是,如果您右键单击没有焦点的项目,则会显示该框并更改焦点条目的日期,而不总是您右键单击的日期。
我找到了这个答案( http://www.vbarchiv.net/tipps/tipp_920-rechtsklick-in-der-standard-listbox-erkennen.html ),但在 VBA 中这是不可能的。有人有 VBA 的解决方案吗?
我实际上需要在框显示之前更改右键单击的焦点项目。
谢谢
最佳答案
这通常是通过列表框不支持的 HitTest 来完成的,这里有一个 hacky 方法;
在表单上的某处添加另一个名为lbTest
的列表框,双击其BorderStyle
属性,直到它看起来像一个空的白色框,设置其visible
到假
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
关于vba - 在 Word VBA 中右键单击选择列表框项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9097525/