如何将水平滚动条添加到 VBA 列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/837860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:46:09  来源:igfitidea点击:

How can I add a horizontal scrollbar to a VBA ListBox

vba

提问by Tom Hennen

I'd like to add a horizontal scrollbar to a VBA ListBox.

我想向 VBA 列表框添加水平滚动条。

It appears that the built in ListBox does not add a horizontal scrollbar automatically. I have a number of fields whose contents exceed the width of the ListBox and are thus unreadable to the user.

似乎内置的 ListBox 不会自动添加水平滚动条。我有许多字段的内容超出了 ListBox 的宽度,因此用户无法读取。

I found this article, however the code fails, due to accessing hwnd of the ListBox (which is apparently not available in VBA). I'd rather not write a native DLL to accomplish this as I suspect there is a better way.

我找到了这篇文章,但是由于访问了 ListBox 的 hwnd(在 VBA 中显然不可用),代码失败了。我宁愿不编写本机 DLL 来完成此操作,因为我怀疑有更好的方法。

Any idea on how I can add a horizontal scrollbar to a VBA ListBox?

关于如何向 VBA 列表框添加水平滚动条的任何想法?

I'm open to the idea of using an alternate control rather than getting it to work with the ListBox specifically.

我愿意使用替代控件而不是让它专门与 ListBox 一起使用。

回答by THEn

Did you try ColumnWidths property? I have listbox with horizontal scroll bar. I just had to add ColumnWidths property.

您是否尝试过 ColumnWidths 属性?我有带有水平滚动条的列表框。我只需要添加 ColumnWidths 属性。

For example I have

例如我有

me.Listbox1.Columnwidts ="0.5 in;0.2 in;1.5 in;0.75 in;0.5 in"

e.Listbox1.Columnwidts ="0.5 in;0.2 in;1.5 in;0.75 in;0.5 in"

回答by Lunatik

Unless I'm missing something, a VBA listbox will automatically gain a horizontal scrollbar if the total of its ColumnWidthsproperty exceeds its own width.

除非我遗漏了某些东西,否则如果 VBA 列表框的ColumnWidths属性总数超过其自身宽度,它会自动获得一个水平滚动条。

There are no properties I know of that affect this behaviour, i.e. I don't otherwise know how to force or disable display of the horizontal scrollbar.

我知道没有任何属性会影响这种行为,即我不知道如何强制或禁用水平滚动条的显示。

回答by Lunatik

Access will automatically add a horizontal scrollbar if the column width exceed the width of the listbox. HOWEVER, if you are using multiple columns, the first column cannot be set to 0. You must have at least some value in there, even if it's just 0.1" Hope this helps.

如果列宽超过列表框的宽度,Access 将自动添加水平滚动条。但是,如果您使用多列,则第一列不能设置为 0。您必须至少有一些值,即使它只是 0.1" 希望这会有所帮助。

回答by Lunatik

Handle to he list box can be obtained as follows :-

可以按如下方式获取列表框的句柄:-

Dim ListHwnd As Integer lstboxName.SetFocus ListHwnd = GetFocus()

Dim ListHwnd As Integer lstboxName.SetFocus ListHwnd = GetFocus()

Use this ListHwnd as the first parameter to the sendmessage function...

使用这个 ListHwnd 作为 sendmessage 函数的第一个参数...

We need to provide the declaration below,Since GetFocus function is not present in VBA by default

我们需要提供以下声明,因为默认情况下 VBA 中不存在 GetFocus 函数

Private Declare Function GetFocus Lib "user32" () As Integer

私有声明函数 GetFocus Lib "user32" () As Integer

回答by user5138047

In Visual Studio 2017, you can click on the list box, then go to the properties panel, and then (scroll down to) find the 'HorizontailScrollbar' property. By default this is property is set to false, so you should set it to true.

在 Visual Studio 2017 中,您可以单击列表框,然后转到属性面板,然后(向下滚动到)找到“Horizo​​ntailScrollbar”属性。默认情况下,此属性设置为 false,因此您应该将其设置为 true。

You know you have set the scroll bar properly when a small triangle appears in the top right corner of the list box.

当列表框的右上角出现一个小三角形时,您就知道您已经正确设置了滚动条。

Hope this helps.

希望这可以帮助。

回答by Alan McBee - MSFT

In that article, the only reason it's getting ScaleMode is to set the width of the horizontal scroll bar. You don't have to do that.

在那篇文章中,它获得 ScaleMode 的唯一原因是设置水平滚动条的宽度。你不必那样做。

SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, 800, 0

where 800 is the pixel width you want the list box to be able to scroll right to.

其中 800 是您希望列表框能够向右滚动到的像素宽度。

You will still need the hWnd. Best bet there is to use an external DLL (written in VB) which can enum through child windows of your process until it finds the windows class for the listbox (you will need to find some way to uniquely identify its parent, such as the window title/text or something). That same DLL could also do the SendMessage call above to set the horizontal text extent (perhaps also it could measure the width of the contained list items).

您仍然需要 hWnd。最好的办法是使用外部 DLL(用 VB 编写),它可以枚举进程的子窗口,直到找到列表框的窗口类(您需要找到某种方法来唯一标识其父级,例如窗口标题/文字或其他东西)。同一个 DLL 也可以执行上面的 SendMessage 调用来设置水平文本范围(也许它还可以测量包含的列表项的宽度)。