如何在单击按钮时动态地将组件添加到 TScrollBox 中?
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
放置在 ScrollBox 上的控件的顶部坐标需要考虑已经发生的“滚动”量。如果您一次添加所有控件,这不是问题,因为 ScrollBox 没有机会“滚动”。
如果在 ScrollBox 有机会“滚动”之后将控件添加到 ScrollBox,则需要考虑发生的垂直“滚动”量。下面是一段示例代码,它将向
ScrollBox1
添加标签,同时考虑垂直滚动,以便控件不会相互重叠。在这里,我使用表单的“Tag”属性来保存添加的下一个控件的Top
,并且我还使用Tag
为标签生成唯一的名称(所以你可以看到它们正在正确的坐标处进入 ScrollBox)。我有时使用的另一种方法是跟踪最后添加的控件,并将新控件的坐标基于最后添加的控件的坐标:
而另一种方法是找到最低的控件并根据它的坐标:
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 theTop
for the next control added, and I'm also usingTag
to generate unique names for the labels (so you can see they're going into the ScrollBox at the correct coordinates).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:
And yet an other approach would be to find the lowest control and add your control based on it's coordinates: