gpt4 book ai didi

vba - 在 Word VBA 中右键单击选择 ListBox 项

转载 作者:行者123 更新时间:2023-12-04 05:59:29 25 4
gpt4 key购买 nike

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

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

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

谢谢

最佳答案

这通常是通过列表框不支持的 HitTest 来完成的,这是一种hacky 方法;

添加另一个名为 lbTest 的列表框在表单的某处,双击它的 BorderStyle属性直到它看起来像一个空的白框,设置它的 visiblefalse

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 中右键单击选择 ListBox 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9097525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com