1. Home
  2. Computing & Technology
  3. Delphi Programming

ListBox with a horizontal scroll bar

By , About.com Guide

To add a horizontal scroll bar to a list box component, use the following procedure:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure SetHorizontalScrollBar(lb : TListBox) ;
var
  j, MaxWidth: integer;
begin
  MaxWidth := 0;
  for j := 0 to lb.Items.Count - 1 do
  if MaxWidth < lb.Canvas.TextWidth(lb.Items[j]) then
    MaxWidth := lb.Canvas.TextWidth(lb.Items[j]) ;

  SendMessage(lb.Handle,
              LB_SETHORIZONTALEXTENT,
              MaxWidth + 5, 0) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
{ Usage: }
SetHorizontalScrollBar(ListBox1) ;

Comment by Peter Bernard:

There is an easier way ... simply use the SCROLLWIDTH property of the listbox. This property gets/sets the logical width of the list box and is measured in pixels. When it becomes greater than the client width of the listbox a horizontal scroll bar will appear.

A simplistic approach would be to simply set this property to the largest LB1.CANVAS.TEXTWIDTH of the listbox items.

I know for a fact that this works in D7, not too sure about the previous versions though.

Delphi tips navigator:
» Detecting and preventing Windows shut down
« Show / Hide Desktop icons

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TListBox
  6. ListBox with a horizontal scroll bar

©2009 About.com, a part of The New York Times Company.

All rights reserved.