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)
You mean u want both hori/vert scrollbars to appear as and when you need it?
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
>> 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
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
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.I tems.Strin gs[i]);
SendMessage(ListBox1.Handl e, 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
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]
MaxWidth := ListBox1.Canvas.TextWidth(
SendMessage(ListBox1.Handl
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.
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
Bor, if you want some help key trapping the ctrl-left and right, just let us know, it is fairly straight forward.
Edo
ASKER
How to post points to EDO?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.