如何在单击按钮时动态地将组件添加到 TScrollBox 中?

发布于 2024-11-18 22:37:24 字数 146 浏览 3 评论 0原文

我创建了一个 TScrollBox。我已经在按钮单击时动态添加了标签和编辑框。为了设置组件的位置,我使用了组件的高度、宽度、左侧、顶部属性。 但是,当添加 5 个组件后滚动条出现在屏幕上时,下一个组件的位置就会受到干扰。并且下一个组件不会以同步方式放置在ScrollBox上。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-11-25 22:37:24

放置在 ScrollBox 上的控件的顶部坐标需要考虑已经发生的“滚动”量。如果您一次添加所有控件,这不是问题,因为 ScrollBox 没有机会“滚动”。

如果在 ScrollBox 有机会“滚动”之后将控件添加到 ScrollBox,则需要考虑发生的垂直“滚动”量。下面是一段示例代码,它将向 ScrollBox1 添加标签,同时考虑垂直滚动,以便控件不会相互重叠。在这里,我使用表单的“Tag”属性来保存添加的下一个控件的 Top,并且我还使用 Tag 为标签生成唯一的名称(所以你可以看到它们正在正确的坐标处进入 ScrollBox)。

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;

我有时使用的另一种方法是跟踪最后添加的控件,并将新控件的坐标基于最后添加的控件的坐标:

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;

而另一种方法是找到最低的控件并根据它的坐标:

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文