Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 556
naren Avatar asked Jul 08 '11 06:07

naren


1 Answers

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;
like image 183
Cosmin Prund Avatar answered Sep 20 '22 04:09

Cosmin Prund