PDA

View Full Version : ListBox, ComboBox Horizontal Scroll: Simple Solution



KevinBarnett
2004-10-08, 06:17 AM
Hi Gang,

Many developers probably know this little trick already. In case you dont, here it is.

There is no horizontal scroll property for the ListBox and ComboBox form controls. If you want to have a horizontal scroll change the value of the ColumnWidths property to a value larger that the width of your control. For example, if the Width is 120, set the ColumnWidths to 350.

Have a great day,

Kevin.

ntaylor
2004-10-08, 07:01 AM
Hi Kevin,

Here is a little extra info for you. You can actually make the scrollbar go exactly to the length of the longest item in the listbox. I achieve it by having a Textbox with the Visible property set to false and the Autosize property set to true and with the same font settings as the listbox. I get the value of the first item in the listbox and set the textbox with this value then set the columnwidth of the listbox to the width of the textbox. I then loop all items in the listbox setting the value of the textbox and if the textbox width is greater than the listbox's columnwidth I update it. Here is my code for a listbox called lstBatch and textbox called txtListScroll that I run after the listbox gets updated.

Regards - Nathan



Private Sub SetScroll()
Dim intListCount As Integer
Dim intCount As Integer
intListCount = lstBatch.ListCount
If intListCount <> 0 Then
txtListScroll = lstBatch.list(0)
lstBatch.ColumnWidths = txtListScroll.Width
End If
intCount = 0
While intCount <> intListCount
txtListScroll = lstBatch.list(intCount)
If lstBatch.ColumnWidths < txtListScroll.Width & " pt" Then
lstBatch.ColumnWidths = txtListScroll.Width
End If
intCount = intCount + 1
Wend
End Sub

KevinBarnett
2004-10-15, 06:52 AM
Thanks Nathan. Its adding tips like this that really make this Forum useful.

Have a great day,

Kevin.