DelphiFAQ Home Search:

Enabling a horizontal scrollbar in a TListBox

 

comments2 comments. Current rating: 4 stars (2 votes). Leave comments and/ or rate it.

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;

Comments:

2008-04-08, 04:50:53
anonymous from South Africa  
rating
You can also create a descendent Tlistbox and expose the columns property:

public
property Columns;

(Or if you have the VCL source, amend and recompile)

2008-08-09, 05:22:32   (updated: 2008-08-09, 05:33:54)
[hidden]  
rating
Helpful but... for example:

var
W: Integer;
begin
W := ListBox1.Canvas.TextWidth(ListBox1.Items[0]);
SendMessage(SrcList.Handle, LB_SETHORIZONTALEXTENT, W, 0);
end;

for some reasom, the above code does not show the expected results. The resulting scroll width isn't enough to enclose a W length text line. Why?

Thanks.

EDITED:

Sorry, but i found out the solution. Maybe this can be considered as a bug.

ListBox1.Canvas.TextWidth('Hello') <> Form1.Canvas.TextWidth('Hello');

I cannot explain why but if you edit Font property of TForm1 even if ListBox1 visually draws texts with the new font, when you attempt to get TextWidth via the ListBox canvas the result are not based on the new font. Maybe because ListBox.Canvas.Font <> ListBox.Font, otherwise bug.

Note: I'm using Delphi 2007. I used Delphi 5 for years and finally i updated to 2007.

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.