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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listbox query

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
GB
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>&nbsp;&nbsp;&nbsp;&nbsp;Dim SaveFontName As String<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim SaveFontSize As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim SaveFontBold As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim SaveFontItalic As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim SaveFontStrikeThru As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim SaveFontUnderline As Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim AvgWidth As Single<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Save form's font settings so we can use the form to calculate the<br>&nbsp;&nbsp;&nbsp;&nbsp;' TextWidth / Height of the strings to go into the list box.<br>&nbsp;&nbsp;&nbsp;&nbsp;SaveFontName = Me.FontName<br>&nbsp;&nbsp;&nbsp;&nbsp;SaveFontSize = Me.FontSize<br>&nbsp;&nbsp;&nbsp;&nbsp;SaveFontBold = Me.FontBold<br>&nbsp;&nbsp;&nbsp;&nbsp;SaveFontItalic = Me.FontItalic<br>&nbsp;&nbsp;&nbsp;&nbsp;SaveFontStrikeThru = Me.FontStrikethru<br>&nbsp;&nbsp;&nbsp;&nbsp;SaveFontUnderline = Me.FontUnderline<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Set form font settings to be identical to list box.<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontName = lb.FontName<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontSize = lb.FontSize<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontBold = lb.FontBold<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontItalic = lb.FontItalic<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontStrikethru = lb.FontStrikethru<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontUnderline = lb.FontUnderline<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Get the average character width of the current list box font<br>&nbsp;&nbsp;&nbsp;&nbsp;' (in pixels) using the form's TextWidth width method.<br>&nbsp;&nbsp;&nbsp;&nbsp;AvgWidth = Me.TextWidth _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;AvgWidth = AvgWidth / Screen.TwipsPerPixelX / 52<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Set the form's font properties back<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontName = SaveFontName<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontSize = SaveFontSize<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontBold = SaveFontBold<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontItalic = SaveFontItalic<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontStrikethru = SaveFontStrikeThru<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.FontUnderline = SaveFontUnderline<br><br>&nbsp;&nbsp;&nbsp;&nbsp;GetAvgFontCharWidth = AvgWidth<br>End Function<br><br>Private Sub SetListBoxWidth()<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim i as Integer<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim MaxCount as Integer<br><br>&nbsp;&nbsp;&nbsp;&nbsp;MaxCount = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;For i = 0 to lb.ListCount<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If Len(lb.item(i)) &gt; MaxCount Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MaxCount = Len(lb.item(i))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;&nbsp;&nbsp;Next i<br><br>&nbsp;&nbsp;&nbsp;&nbsp;lb.Width = (MaxCount * SavedAvgCharWidth) + 20<br>&nbsp;&nbsp;&nbsp;&nbsp;' (Add 20 for fudge factor)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;&nbsp;what property is there for the List box to give the value from the Hscroll control.&nbsp;&nbsp;I'm just wanting to link the Scroll bar to the list box.<br><br>Thanks for your help!<br><br><img src=
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top