Board index » delphi » TIP: How to add a horizontal scrollbar to a ListBox

TIP: How to add a horizontal scrollbar to a ListBox

I am posting the following just because I thought that some of you might find
it useful.  

There is a component that I wrote some time ago which has a TListBox on
it. It's a user-draw TListBox which displays a small bitmap and then some
text.  Sometimes the text is to long to fit and runs past the width of the
list box so that the user can't see it.  When I initially wrote the component,
I had very little time and thought that adding a scroll bar would take a lot
of component development, so I put it off.  But it turns out that it takes
very little code and doesn't require subclassing the list box.  

The key to it is the LB_SETHORIZONTALEXTENT message.  Just this message to
your list box with the maximum width required by your list box and a scroll
bar will appear.  The following code is the code that I use in my component to
ensure that the scrollbar is the correct width.  If you do not have an owner
draw list box (no bitmap) then just remove the associated code.  

I hope that this helps someone.

  MaxWidth := 0;
  for i := 0 to ListBox1.Items.Count - 1 do    { Iterate }
  begin
    pBitmap := ListBox1.Items.Objects[i]; { get the bitmap }
    if pBitmap = nil then
        pBitmap := Pointer (FEmptyBitmap);

    if pBitmap <> nil then
      Width := TBitmap (pBitmap).Width + ListBox1.Canvas.TextWidth (
        ListBox1.Items.Strings[i])
    else
      raise Exception.Create ('Bitmap missing');
    if Width > MaxWidth then
      MaxWidth := Width;
  end;
  SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth, 0);

--
Ron Denis
E-mail : rde...@lucent.com
Consultant
Lucent Technologies Inc.

 

Re:TIP: How to add a horizontal scrollbar to a ListBox


Quote
>I am posting the following just because I thought that some of you might find
>it useful.  
>There is a component that I wrote some time ago which has a TListBox on
>it. It's a user-draw TListBox which displays a small bitmap and then some
>text.  Sometimes the text is to long to fit and runs past the width of the
>list box so that the user can't see it.  When I initially wrote the component,
>I had very little time and thought that adding a scroll bar would take a lot
>of component development, so I put it off.  But it turns out that it takes
>very little code and doesn't require subclassing the list box.  
>The key to it is the LB_SETHORIZONTALEXTENT message.  Just this message to
>your list box with the maximum width required by your list box and a scroll
>bar will appear.  The following code is the code that I use in my component to
>ensure that the scrollbar is the correct width.  If you do not have an owner
>draw list box (no bitmap) then just remove the associated code.  
>I hope that this helps someone.
>  MaxWidth := 0;
>  for i := 0 to ListBox1.Items.Count - 1 do    { Iterate }
>  begin
>    pBitmap := ListBox1.Items.Objects[i]; { get the bitmap }
>    if pBitmap = nil then
>        pBitmap := Pointer (FEmptyBitmap);
>    if pBitmap <> nil then
>      Width := TBitmap (pBitmap).Width + ListBox1.Canvas.TextWidth (
>        ListBox1.Items.Strings[i])
>    else
>      raise Exception.Create ('Bitmap missing');
>    if Width > MaxWidth then
>      MaxWidth := Width;
>  end;
>  SendMessage(ListBox1.Handle, LB_SETHORIZONTALEXTENT, MaxWidth, 0);
>--

Nice tip.  Thanks.

Here's another twist for you:  I've got a list-box that displays a horizontal
scroll-box although the effect of this box is to scroll the data vertically.  
B'damned if I know why.  

Why?  :-)

Other Threads