tags:

views:

3350

answers:

5

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

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.

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.

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

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

A: 

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.

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.

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).

Alan McBee
A: 

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

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.

Lunatik
+1  A: 

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

For example I have

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

THEn
+1  A: 

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.

A: 

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

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

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

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

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