Coordinates in a TScrollBox

I am writing a component that includes a TScrollBox. Into this ScrollBox I put several labels (TLabel). After inserting a Label I increase the position variables (CurrentX, CurrentY) to identify the position for the next label that may be inserted.
All this works fine if I do not scroll the client area of the ScrollBox.
If I scroll it e.g. downwards, the next label I insert will be placed incorrectly. CurrentX and CurrentY seem to be the absolute coordinates of the clientArea of the ScrollBox.
How can I convert them into logical coordinates to place the labels correctly, even if I scroll the field?
klusiAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hi klusi,

what about this

var
  X : Integer = 10;
  Y : Integer = 10;

procedure TForm1.Button1Click(Sender: TObject);
var L : TLabel;
begin
  L := TLabel.Create(Self);
  L.Top := Y - ScrollBox1.VertScrollBar.Position;
  L.Left := x;
  L.Caption := 'Label at '+inttostr(x)+':'+inttostr(y));
  L.Parent := ScrollBox1;
  Y := Y + l.Height + 2;  //prepare for next line
end;

meikl
0
 
intheCommented:
How are you getting/setting the positions?
something along the lines of
label[i].top := label[ii].top-label[ii].height;
should work where ii is last label inserted,i is new label..we did something on a question here like this adding panels to the scrollbox.
0
 
intheCommented:
remember also you can set the labels align property to alTop
as each label is added they will be added directly below the last label.
0
 
klusiAuthor Commented:
Thank you. I tried it out and it worked perfectly.
0
All Courses

From novice to tech pro — start learning today.