Author: William Gerbert
Enabling a horizontal scrollbar in a TListBox
Answer:
Solve 1:
There is no such property in TListBox. To force a listbox to have horizontal
scrollbars, use the message LB_SETHORIZONTALEXTENT.
1 // e.g. in FormCreate(..)2 begin3 ListBox1.Width := 300;
4 // listbox can be scrolled by 100 pixels horizontally now:5 SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, 400, 0);
6 end;
Solve 2:
7 MaxWidth := 0;
8 for i := 0 to ListBox1.Items.Count - 1 do9 if MaxWidth < ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]) then10 MaxWidth := ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]);
11 SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth + 100, 0);
It uses the Messages .dcu.