Visual Basic List API Routines
SendMessage: Display the List Box Horizontal Scroll Bar
Posted:   Thursday October 7, 1999
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch


SendMessage: Display the List Box Horizontal Scroll Bar if Needed
SendMessage: Align List Box Contents Using Tabstops
VBnet CoolTabs

This is a simple yet effective routine to calculate the width of the longest item in a list box, and if partially obscured by the listbox edge or the scrollbars, add a horizontal scrollbar to the control.
 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Public Const DT_CALCRECT = &H400
Public Const SM_CXVSCROLL = 2

Public Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Public Declare Function DrawText Lib "user32" _
   Alias "DrawTextA" _
  (ByVal hDC As Long, _
   ByVal lpStr As String, _
   ByVal nCount As Long, _
   lpRect As RECT, ByVal _
   wFormat As Long) As Long
Public Declare Function GetSystemMetrics Lib "user32" _
  (ByVal nIndex As Long) As Long

Public Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long
 Form Code
Add a list (List1) and a command button (Command1) to a form, along with the following code:

Option Explicit

Private Sub Command1_Click()

   Dim c As Long
   Dim rcText As RECT
   Dim newWidth As Long
   Dim itemWidth As Long
   Dim sysScrollWidth As Long
  'assure that the form font is the same as the
  'list font to assure the DrawText method
  'calculates the width correctly.
   Form1.Font.Name = List1.Font.Name
   Form1.Font.Bold = List1.Font.Bold
   Form1.Font.SIZE = List1.Font.SIZE
  'get the width of the system scrollbar
   sysScrollWidth = GetSystemMetrics(SM_CXVSCROLL)
  'loop through the list items, using DrawText
  'with DT_CALCRECT to determine the longest item.
   For c = 0 To List1.ListCount - 1
      Call DrawText(Form1.hDC, (List1.List(c)), -1&, rcText, DT_CALCRECT)
     'calc the required width to display the
     'widest list item by adding the rect
     'width needed to display the item
     'with the width of the system scroll bar
      itemWidth = rcText.Right + sysScrollWidth
     'if this width is wider than a previous
     'value, save the longer width
      If itemWidth >= newWidth Then
         newWidth = itemWidth
      End If
  'add a horizontal scrollbar wide enough
  'to display the longest list item. If the
  'scrollbar is not needed, its not shown.
   Call SendMessage(List1.hwnd, LB_SETHORIZONTALEXTENT, newWidth, ByVal 0&)
End Sub

Private Sub Form_Load()

   List1.AddItem "Ministry of Agriculture and Food"
   List1.AddItem "Ministry of the Attorney General"
   List1.AddItem "Ministry of Community, City and Social Services"
   List1.AddItem "Ministry of Education"
   List1.AddItem "Ministry of Funny Walks"
   List1.AddItem "Ministry of the Environment"
   List1.AddItem "Ministry of Health and Long-Term Care"
   List1.AddItem "Ministry of Housing"
   List1.AddItem "Management Board of Cabinet"
   List1.AddItem "Ministry of Natural Resources"
   List1.AddItem "Ministry of the Solicitor General"
   List1.AddItem "Ministry of Transportation"
   List1.AddItem "Fine art"
   List1.AddItem "Cover design and artwork"
   List1.AddItem "Stat camera work/camera-ready art"
   List1.AddItem "Photograph cropping/sizing"
   List1.AddItem "Custom paste-up and hand layouts"
   List1.AddItem "Poster session layout and mounting"
   List1.AddItem "Display design and mock-ups"
   List1.AddItem "Display/photo mounting and framing"
   List1.AddItem "Overhead mounting"
   List1.AddItem "Pre-press graphic art mock-ups"
   List1.AddItem "Photocopying / assembly / binding"
   List1.AddItem "Maintenance/supplies orders"

End Sub
This routine calculates in pixels, and so is resolution-independent. This routine will produce the exact same results on all display resolutions.  That is to say, if a horizontal scrollbar is needed at 640x480, it will correctly determine if one is needed at 1024x768.

If your application uses a list to display data that you provide (as opposed to load at runtime from the users entry), and you therefore always know what the length of the longest item will be, you can use forego the preparation steps and directly assign (hard-code) the appropriate value as newWidth.

If you require a dynamic approach, see SendMessage: Display the List Box Horizontal Scroll Bar if Needed.

There is no way to add a horizontal scroll bar to a FileList control.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter