tags:

views:

382

answers:

1

Hi,

I have to use TScrollbar on a TScrollBox(Yes,it seems bad idea,but I have to) that scrolls the box.

This is what I did:I added the scroll on the box ,used Top and Bottom anchors to keep it on track and added this code in the OnScroll event of the ScrollBar:

procedure TForm1.ScrollBar1Scroll(Sender: TObject; 
                                          ScrollCode: TScrollCode;
                                          var ScrollPos: Integer);
begin
  ScrollBox1.ScrollBy(0,-scrollPos);
end;

It's not working properly.Always scrolls down.How to make it scroll normally?

EDIT: Please do not suggest using the bars from TScrollBox,I have to use a TScrollBar.

Thank you!

+4  A: 

The problem is that you're giving the ScrollBox the ScrollBar's absolute scroll position, where the expected parameter is a "delta" or change in position

Here's a quick-and-dirty demonstration solution. You could make this cleaner by creating a different descendant, etc, depending on your needs.

After dropping in your ScrollBox and ScrollBar, add this type declaration above your form's type declaration in the interface section:

type
  TScrollBar = class(StdCtrls.TScrollBar)
  private
    OldPos : integer;
  end;

Now use this for the OnScroll event for the ScrollBar:

procedure TForm1.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
var
  Delta : integer;
begin
  Delta := ScrollPos - ScrollBar1.OldPos;
  ScrollBox1.ScrollBy(0, -Delta);
  ScrollBar1.OldPos := ScrollPos;
end;

This works for me (D2006 under WinXP).

Argalatyr
I'll vote this when my vote limit isn't reached,sorry.I did a similiar to this thing. :)
John