【问题标题】:Select ListBox item on rightclick in Word VBA在 Word VBA 中右键单击选择 ListBox 项
【发布时间】:2012-02-01 14:29:31
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: vba listbox ms-word right-click


    【解决方案1】:

    这通常是通过列表框不支持的命中测试来完成的,这是一种 hacky 方式;

    在表单的某处添加另一个名为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
    

    【讨论】:

    • 哇,这太酷了。非常感谢您快速而有能力的回答。一个简短的测试完全显示了预期的结果。
    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 2011-11-21
    • 2022-01-09
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    相关资源
    最近更新 更多