~~~~~~~~~~~~~~~~~~~~~~~~~
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

