How to add components to TScrollBox dynamically one below another on button click?
Asked Answered
R

1

8

I have created one TScrollBox. I have added the Label and Edit Box on it dynamically on Button click. For setting the location of component i have used the height,width,left,top property of components. But when Scroll Bar gets appeared on screen after 5 components added, the next components location gets disturbed. and the next component is not placed in synchronous manner on ScrollBox.

Renewal answered 8/7, 2011 at 6:32 Comment(2)
There should be some property of the ScrollBox, like ScrollTop or ScrollWhatever. You can just subtract it from the new position of the component.Wesley
I am unable to find any property like ScrollTop.. :(Renewal
A
12

The Top coordinate for controls placed on a ScrollBox need to take into account the amount of "scroll" that already took place. If you add the controls all at once this is not a problem, because the ScrollBox doesn't get the chance to "scroll".

If you add controls to the ScrollBox after it got a chance to "scroll", you need to take into account the amount of vertical "scroll" that took place. Here's a sample piece of code that will add labels to ScrollBox1, taking vertical scroll into account so controls don't overlap each other. Here I'm using the form's "Tag" property to hold the Top for the next control added, and I'm also using Tag to generate unique names for the labels (so you can see they're going into the ScrollBox at the correct coordinates).

procedure TForm31.Button1Click(Sender: TObject);
var L: TLabel;
begin
  L := TLabel.Create(Self);
  L.Caption := 'Test: ' + IntToStr(Tag);
  L.Parent := ScrollBox1;
  L.Top := Tag + ScrollBox1.VertScrollBar.Size - ScrollBox1.VertScrollBar.Position;
  Tag := Tag + L.Height;
end;

An other approach I sometimes used is to keep track of the last control added and base the coordinates for the new control on the coordinates of that last added control:

var LastControl: TControl;

procedure TForm31.Button1Click(Sender: TObject);
var L: TLabel;
begin
  L := TLabel.Create(Self);
  L.Caption := 'Test: ' + IntToStr(Tag);
  L.Parent := ScrollBox1;
  if Assigned(LastControl) then
    L.Top := LastControl.Top + LastControl.Height
  else
    L.Top := 0;
  Tag := Tag + L.Height;

  LastControl := L;
end;

And yet an other approach would be to find the lowest control and add your control based on it's coordinates:

procedure TForm31.Button1Click(Sender: TObject);
var L: TLabel;
    Bottom, TestBottom: Integer;
    i: Integer;
begin
  // Find "Bottom"
  Bottom := 0;
  for i:=0 to ScrollBox1.ControlCount-1 do
    with ScrollBox1.Controls[i] do
    begin
      TestBottom := Top + Height;
      if TestBottom > Bottom then
        Bottom := TestBottom;
    end;
  L := TLabel.Create(Self);
  L.Caption := 'Test: ' + IntToStr(Tag);
  L.Parent := ScrollBox1;
  L.Top := Bottom;
  Tag := Tag + L.Height;
end;
Azikiwe answered 8/7, 2011 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.