Mirror

Enabling a horizontal scrollbar in a TListBox (Views: 14844)


Problem/Question/Abstract:

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.

// e.g. in FormCreate(..)
begin
  ListBox1.Width := 300;
  // listbox can be scrolled by 100 pixels horizontally now:
  SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, 400, 0);
end;


Solve 2:

MaxWidth := 0;
for i := 0 to ListBox1.Items.Count - 1 do
  if MaxWidth < ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]) then
    MaxWidth := ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]);
SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth + 100, 0);

It uses the Messages .dcu.

<< Back to main page