Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TListBox ScrollWidth setting

Status
Not open for further replies.

Opieo

Programmer
Jul 26, 2006
454
GB
Okay, so I have a tiny function that is meant to set the [blue]ScrollWidth[/blue] of a [blue]TListBox[/blue].
However, it does not seem to work when any string in it is long.
Code:
  j := 0;  // Set scroll width for terms
  for i := 0 to (ListBox1.Items.Count - 1) do
   if ((ListBox1.Canvas.TextWidth(ListBox1.Items[i]) + 15) > j) then
    j := (ListBox1.Canvas.TextWidth(ListBox1.Items[i]) + 15);
  ListBox1.ScrollWidth := j;

Both i and j are locally declared integers.
Any thoughts?

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Sorry, I should have specified further. By 'not work' I mean the [blue]ScrollWidth[/blue] is not set large enough. So it will scroll, but some text of the longest strings is still beyond what you can scroll to see.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Made a slight optimization:
Code:
 j := 0;  // Set scroll width for terms
  for i := 0 to (ListBox1.Items.Count - 1) do
   if (ListBox1.Canvas.TextWidth(ListBox1.Items[i]) > j) then
    j := ListBox1.Canvas.TextWidth(ListBox1.Items[i]);
  ListBox1.ScrollWidth := j + 15;

Not sure why it wasn't that way to begin with. Already resized my string grids this way.
But they work just fine; another reason I do not understand why this doesn't work for very large strings.


~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
This seems to work for me:
Code:
[b]ListBox1.Canvas.Font := ListBox1.Font;[/b]
j := 0;  // Set scroll width for terms
for i := 0 to (ListBox1.Items.Count - 1) do
  if (ListBox1.Canvas.TextWidth(ListBox1.Items[i]) > j) then
    j := ListBox1.Canvas.TextWidth(ListBox1.Items[i]);
ListBox1.ScrollWidth := j + 15;

Andrew
Hampshire, UK
 
That fixed it. Thank you much.

~
Give a man some fire, he will be warm for a day, Set a man on fire, he will be warm for the rest of his life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top