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

发布于 2025-01-02 05:50:38 字数 471 浏览 0 评论 0原文

我正在使用 VBA 在 Word 2003 中开发一个项目。我有一个带有一些条目(日期)的多选列表框。右键单击时,我希望弹出一个输入框,用户可以在其中更改所选日期。只要特定项目已经被聚焦(不仅被选择,而且被聚焦),这种方法就很有效。但是,如果您右键单击没有焦点的项目,则会显示该框并更改焦点条目的日期,而不总是您右键单击的日期。

我找到了这个答案(http://www. vbarchiv.net/tipps/tipp_920-rechtsklick-in-der-standard-listbox-erkennen.html),但这在 VBA 中是不可能的。有谁有VBA的解决方案吗?

实际上,我需要在框显示之前更改右键单击的焦点项目。

谢谢

I'm developping a project in Word 2003 with VBA. I have a multiselect ListBox with some entries (dates). On rightclick I'd like to have an InputBox popping up where the user can change the selected date. This works well, as long a specific item is already focused (not only selected, but focused). But, if you rightclick on an item without focus, the box shows up and changes the date of the focused entry, not always the one you rightclicked.

I found this answer (http://www.vbarchiv.net/tipps/tipp_920-rechtsklick-in-der-standard-listbox-erkennen.html) but it's not possible in VBA. Has anyone a solution for VBA?

I actually need to change the focused item on rightclick, before the box shows up.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像极了他 2025-01-09 05:50:38

这通常是通过列表框不支持的命中测试来完成的,这里有一个 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

This is usually done with Hit Testing which Listboxes don't support, here is a hacky way;

Add another listbox called lbTest somewhere on the form, double click its BorderStyle property until it looks like an empty white box, set its visible to 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文