Library code snippets

ListBoxHScroll class

Description

The attached code contains a class that can be used to add items to the ListBox control. When you add a new item, it adds a horizontal scroll bar to a specified list box (if required) and scrolls down the list box so you can see the last added item.

The code uses the SendMessage API function to add a horizontal scroll bar dynamically to a list box using the LB_SETHORIZONTALEXTENT message; it also takes into account the width of the vertical scroll bar that can be present in the list box. To precisely calculate the width of a new item, our class uses the DrawText API function with the DT_CALCRECT flag. Draw attention at the fact that our class works properly if you change the font of the listbox as you want. We use the IFont interface and its hFont property to retrieve the handle of the font used in the listbox. In addition, our class also takes into account the visibility of the vertical scroll bar in the listbox. It determines whether this scroll bar is present retrieving the listbox style flags and testing these set of flags for WS_VSCROLL.

Using of this class called CLBHscroll in real-world applications is very easy. All you need to do is (1) to initialize this class using the Init method which accepts the reference to the list box you want to populate and (2) call the AddItem method of this class to add a new item. The Init method also clears the list box. To see how this class works, create a new exe project in VB and place the CommandButton and ListBox controls on its form. Don't change the default names of these controls (Command1 and List1 respectively). Put the following code in the Command1_Click event procedure:

Private Sub cmdPopulate_Click()
   Dim LBHS As New CLBHScroll
   Dim i As Long, lStrLen As Long
   
   With List1.Font
      .Name = "Arial"
      .Size = 12
      .Italic = True
   End With
   
   LBHS.Init List1
   
   For i = 1 To 30
      lStrLen = Int(Rnd * 50) + 1
      LBHS.AddItem String(lStrLen, "W") + "!"
   Next
End Sub

Run the project and press the Command1 button. You will see that the listbox named List1 is populated with 30 random length strings, has the horizontal scroll bar and displays the last added string.

The attached class can be useful in many real-world situations. For instance, if you perform context search in files, you can use this class to add found files to a list box at the screen as they are found. Our company uses this code in demo applications of the xDir library that allows you to enumerate files and folders in a specified folder and all of its subfolders using various filter criteria (file and folder mask; file size; attributes; date and time of creation, last access and modification, etc.) You can visit our Web-site (www.10Tec.com) to download this and other demos to see how it works.

Comments

  1. 10 Nov 2004 at 13:37
    Can this code be modified for a Combobox? In particular I want to disable a specified item in the combobox, i.e. grey it out so that it is unselectable...

    Thanks,

    Tim
  2. 01 Jan 1999 at 00:00

    This thread is for discussions of ListBoxHScroll class.

Leave a comment

Sign in or Join us (it's free).

10Tec Company

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!