How to Add a Horizontal Scroll Bar to Visual Basic List Box

ID Number: Q80190

1.00

WINDOWS

Summary:

The normal list box that comes with Visual Basic does not have a

horizontal scroll bar. This can be a problem when the item in a list

box extends past the boundaries of the list box. To add a horizontal

scroll bar to the control, you can call the Windows 3.0 API SendMessage

function with the LB_SETHORIZONTALEXTENT (WM_USER + 21) constant.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

To add a horizontal scroll bar to a list box, perform a SendMessage

function call with the LB_SETHORIZONTALEXTENT constant.

This message sets the width in pixels by which a list box can scroll

horizontally. If the size of the list box is smaller than this value,

the horizontal scroll bar will horizontally scroll items in the list

box. If the list box is large as or larger than this value, the

horizontal scroll bar is disabled.

The parameters for the SendMessage function are as follows:

SendMessage(hWnd%, LB_SETHORIZONTALEXTENT, wParam%, lParam&)

------------------------------------------------------------

hWnd% - Handle to the list box

wParam% - Specifies the number of pixels by which the list

box can be scrolled

lParam% - Is not used

To make a program example that will only allow the user to scroll a

specified distance, create a form with the following controls:

Control CtrlName

------- --------

Command button Command1

List box List1

Add the following code in the described locations in your code:

'======== GLOBAL.BAS ==================

Declare Function SendMessage& Lib "user" (

ByVal hWnd%,

ByVal wMsg%,

ByVal wParam%,

ByVal lParam&)

Declare Function GetFocus Lib "User" () as Integer

'======== Form1 =======================

'Note: All commands are each on only one line.

Sub Command1_Click ()

Const LB_SETHORIZONTALEXTENT = &H400 + 21

Const NULL = &O0

'wParam is in PIXEL(3)

ScaleMode = 3

'get the handle

List1.SetFocus

ListHwnd% = GetFocus()

'this string will show up initially

ListString1$ = "Derek is a great "

'you can scroll to see this portion

ListString2$ = "little boy "

'you cannot scroll to see this string

ListString3$ = "but can be a problem sometimes"

ExtraPixels% = TextWidth(ListString2$)

BoxWidth% = TextWidth(ListString1$)

'resize the text box

List1.Move List1.Left, List1.Top, BoxWidth%

'add the scroll bar

X& = SendMessage(ListHwnd%, LB_SETHORIZONTALEXTENT,

BoxWidth% + ExtraPixels%, NULL)

'add the example string to the list box

List1.AddItem ListString1$ + ListString2$ +

ListString3$

End Sub

Additional reference words: 1.00 scrollbar