添加到带有位置的可滚动面板
我正在使用 Panel
来保存控件列表(用户定义)。添加面板的方式是,在将控件添加到面板之前,我会根据 Panel.Controls.Count
设置控件的位置。
comRec.Location = new Point(comRec.Location.X, panel1.Controls.Count * 25);
panel1.Controls.Add(comRec);
现在,效果很好,看起来完全符合我想要的效果。但是,一旦我们达到窗口的限制,AutoScroll
就会启用(我确实想要)。现在,如果用户滚动到面板底部,这最终会更改面板中每个控件的位置。我的第一个 comRec.Location 不是 (0,0),而是类似 (0,-219)。所以现在,当用户添加另一个 comRec 对象时,它会在对象之间创建一个巨大的间隙。
我的问题是,考虑滚动条位置变化并仍然使用我的添加系统的最佳方法是什么。我假设必须检查滚动条的值并使用它来确定位置。
另外,有没有更好的方法来显示控件列表?我应该使用面板
吗?
I am using a Panel
to hold a list of controls (user-defined). The way that I add the panels, I am setting the location of the control based on the Panel.Controls.Count
before I add it to the panel.
comRec.Location = new Point(comRec.Location.X, panel1.Controls.Count * 25);
panel1.Controls.Add(comRec);
Now, this works nicely and looks exactly the way that I want it to. However, once we reach the limit on the window, the AutoScroll
enables (which I do want). Now, if the user were to scroll to the bottom of the Panel
, this ultimately changes the location of every control in the panel. Instead of my first comRec.Location
being (0,0), it is something like (0,-219). So now, when the user adds another comRec
object, it creates a HUGE gap between the objects.
My question is this, what is the best way to account for the changes of the location with the scrollbar and still using my adding system. I am assuming that will have to do something with checking the value of the scrollbar and using it to determine the location.
Also, is there a BETTER way to display a list of controls? Should I be using a Panel
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看 FlowLayoutPanel 控件,它正是这样的你什么。
Look at the FlowLayoutPanel control, it's exactly what you what.
您可以在层次结构中添加一个附加面板:
这样,附加控件的位置将相对于它们的直接父级(非滚动面板)。
You could add an additional panel into the hierarchy:
This way, your additional controls' locations would be relative to their direct parent, the non-scrolling panel.
如果您添加多个控件,请尝试在添加控件时暂停面板的布局:
这在用户可以动态更改现有控件的可见性的类似情况下对我有所帮助。
If you add several controls, try to suspend the layout of the panel while adding the controls:
This helped me in a similar situation where the user could change dynamically the visibility of existing controls.