Automatically Scroll Down/Add Horizontal Scroll Bar to List Box

Category:
Forms and Controls
Type:
Classes
Difficulty:
Intermediate
Author:
10Tec Company

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
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 TextWidth method of the form that contains the specified list box. This means that the list box must use the same font as the form.

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.

The attached can be useful in many real-world situations. For instance, if you perform context search in files, you can use this class to add to a list box at the screen found files 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.


Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.

Download listboxhscrolldemo.zip