vba - 在 Word VBA 中右键单击选择列表框项

标签 vba listbox ms-word right-click

我正在使用 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/

相关文章:

vba - 如何检查单元格值中的第一个字符

excel - 确定输入的字符串是否带有前导单引号

c# - 使用 wpf 在列表框中搜索

c# - 在当前 docx 文件上执行 word 加载项

javascript - 如何编写打印 css 以几乎与我们从 MS word 获得的方式相同的方式从网页获得打印?

excel - 如果被调用的子调用 `Exit Sub`,则退出主子

excel - 将 "dd.mm.yyyy"更改为 "mm/dd/yyyy"VBA

c# - 如何在列表框中的项目之间添加填充?

C# 列表框 : Display custom items in listbox

c# - Word.Application.Selection.Find.Execute 不适用于 Word 2013