Is there a property to make a list box's width fit to the size of a data item??<br><br>If not do i have to use the Hscroll bar, and if I do how do still find the width of the item in my listbox?<br><br>Any help appreciated<br><br>
It's not pretty, but what I've done in the past is something (but not exactly) like this:<br><FONT FACE=monospace><br>Private Function GetAvgFontCharWidth() as Single<br> Dim SaveFontName As String<br> Dim SaveFontSize As Integer<br> Dim SaveFontBold As Integer<br> Dim SaveFontItalic As Integer<br> Dim SaveFontStrikeThru As Integer<br> Dim SaveFontUnderline As Integer<br> Dim AvgWidth As Single<br><br> ' Save form's font settings so we can use the form to calculate the<br> ' TextWidth / Height of the strings to go into the list box.<br> SaveFontName = Me.FontName<br> SaveFontSize = Me.FontSize<br> SaveFontBold = Me.FontBold<br> SaveFontItalic = Me.FontItalic<br> SaveFontStrikeThru = Me.FontStrikethru<br> SaveFontUnderline = Me.FontUnderline<br><br> ' Set form font settings to be identical to list box.<br> Me.FontName = lb.FontName<br> Me.FontSize = lb.FontSize<br> Me.FontBold = lb.FontBold<br> Me.FontItalic = lb.FontItalic<br> Me.FontStrikethru = lb.FontStrikethru<br> Me.FontUnderline = lb.FontUnderline<br><br> ' Get the average character width of the current list box font<br> ' (in pixels) using the form's TextWidth width method.<br> AvgWidth = Me.TextWidth _<br> ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"<br> AvgWidth = AvgWidth / Screen.TwipsPerPixelX / 52<br><br> ' Set the form's font properties back<br> Me.FontName = SaveFontName<br> Me.FontSize = SaveFontSize<br> Me.FontBold = SaveFontBold<br> Me.FontItalic = SaveFontItalic<br> Me.FontStrikethru = SaveFontStrikeThru<br> Me.FontUnderline = SaveFontUnderline<br><br> GetAvgFontCharWidth = AvgWidth<br>End Function<br><br>Private Sub SetListBoxWidth()<br> Dim i as Integer<br> Dim MaxCount as Integer<br><br> MaxCount = 0<br> For i = 0 to lb.ListCount<br> If Len(lb.item(i)) > MaxCount Then<br> MaxCount = Len(lb.item(i))<br> End If<br> Next i<br><br> lb.Width = (MaxCount * SavedAvgCharWidth) + 20<br> ' (Add 20 for fudge factor)<br><br> End Sub<br></font><br><br>So, you'd call GetAvgCharWidth in your FormLoad to find out how big the font is, then call SetListBoxWidth after you've loaded it up.<br><br>BTW: Don't expect to cut & paste this into your project, I'm not 100% sure it will compile as is.<br><br>Chip H.<br>
Thanks for that but that will just make the width of the list box larger.<br><br>I need the Hscroll control to work on a list box what property is there for the List box to give the value from the Hscroll control. I'm just wanting to link the Scroll bar to the list box.<br><br>Thanks for your help!<br><br><img src=
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.