Link to home
Start Free TrialLog in
Avatar of bor061297
bor061297

asked on

ListBox Scroll

Besides its automatic behaviour, what command should I use to scroll content of ListBox during run-time, whenever I want? (horizontaly, verticaly)
Avatar of mikepj
mikepj
Flag of Canada image

ListBox.TopIndex:=whatever
I don't know how to do the horizontal scrolling of a listbox but there must be a way.  Besides, I don't know why you would want to be able to do that programmatically.
Avatar of kjteng
kjteng

You mean u want both hori/vert scrollbars to appear as and when you need it?

Avatar of bor061297

ASKER

>>I don't know how to do the horizontal scrolling of a listbox
>> but there must be a way.  Besides, I don't know why you would >>want to be
>>able to do that programmatically.

I want to make a ListBox that allows user to scrool it left/right with CTRL+ LEFT/RIGHT CRSR
I think I know how to do that.
please reject the current answer so that I can post mine
I think I know how to do that.
please reject the current answer so that I can post mine
Delphi's TListBox component automatically implements a vertical scrollbar. The scrollbar appears when the list box isn't tall enough to display all its items. However, the list box doesn't display a horizontal scrollbar when its items are wider than its width. Of course, there's a way for you to add the horizontal scrollbar.

Add the following code to the OnCreate event of your form:

procedure TForm1.FormCreate(Sender: TObject);
var
  i, MaxWidth: integer;
begin
  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+2, 0);
end;

The code finds the width, in pixels, of the longest string in the list box. Then, it uses the LB_SETHORIZONTALEXTENT message to set the horizontal scrollable width, in pixels, of the list box. The two extra pixels added to MaxWidth help offset narrow characters from the right edge of the list box.

If you want to set a scroll bar to a particular line:

  SelStart := Perform( EM_LINEINDEX, LineNumber, 0 );
  Perform( EM_SCROLLCARET, 0, 0 );

If you look up the EM_ functions you'll find everything you need for concise control of your listbox scrollbars.

HTH

Edo

use the sendMessage as follows (w is the width to scroll in pixels):
sendMessage( listbox1.handle, lb_sethorizontalExtent, w ,0)

My answer is similar to that of Edo. Since he has posted the comment, he should get the points.


Thanks, kjetng.

Bor, if you want some help key trapping the ctrl-left and right, just let us know, it is fairly straight forward.

Edo
How to post points to EDO?
ASKER CERTIFIED SOLUTION
Avatar of Edo082297
Edo082297

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial