Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autofit Columns in ListBox 1

Status
Not open for further replies.

PeasNCarrots

Programmer
Jun 22, 2004
73
US
Is there a way to AutoFit Columns in ListBox? My list box recordsource changes frequently and I don't want to be coding in the column widths for each query results.

Thanks

Patrick
 
There is no Autofit feature in the ListBox control.

You could write your own but it would pretty complex as it would need to determine the size of the longest text for each column, including the headers if displayed, in your measurement units (cms or ins) and then apply some rule about the width of the ListBox and whether you allowed a horizontal scroll or not. It would also need to know if the first column was to be hidden as is commonly the case.

There is an API call to return you the length of a given piece of text is that helps.

All in all it's probably a few hours work and only you can determine if it's worth it.

 
Thanks for all your help. It is not worth it for me to do it. Afterall, the data will be exported to excel.
 
Hi..

Saw your problem as interesting...

This will work

Option Compare Database

Private Sub Form_Activate()
Dim templenght, x As Integer
templenght = Len(List0.ItemData(0)) ' Set to the lenght of the first item in the list
Me.List0.Enabled = False ' disable list box
For x = 0 To List0.ListCount - 1 ' loop to find the longest entry in the field
If Len(List0.ItemData(x)) > templenght Then templenght = Len(List0.ItemData(x))
Next x
Me.List0.Width = 1440 * templenght / 14 'Width is set in twips, 1440 twips is an inch / 14 characters per inch using the standard font
Me.List0.ColumnWidths = 1440 * templenght / 14 ' same as above, must set the properties in this order
Me.List0.Enabled = True ' enable list box
Refresh
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top