在Word VBA中右键单击选择列表框项目 [英] Select ListBox item on rightclick in Word VBA

查看:234
本文介绍了在Word VBA中右键单击选择列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VBA在Word 2003中开发一个项目.我有一个带有某些条目(日期)的多选ListBox.右键单击,我想弹出一个输入框,用户可以在其中更改所选日期.只要特定项目已经集中(不仅已选定,而是集中),此方法就可以很好地工作.但是,如果右键单击没有焦点的项目,则会显示该框并更改焦点条目的日期,而并非总是右键单击的条目.

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.

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

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.

谢谢

推荐答案

这通常是通过列表框不支持的点击测试完成的,这是一种骇人听闻的方式;

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

在窗体上的某处添加另一个名为lbTest的列表框,双击其BorderStyle属性,直到它看起来像一个空白框,将其visible设置为false

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

这篇关于在Word VBA中右键单击选择列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆