添加到带有位置的可滚动面板

发布于 2024-10-26 19:59:57 字数 558 浏览 5 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

心的位置 2024-11-02 19:59:57

看看 FlowLayoutPanel 控件,它正是这样的你什么。

Look at the FlowLayoutPanel control, it's exactly what you what.

埖埖迣鎅 2024-11-02 19:59:57

您可以在层次结构中添加一个附加面板:

Outer panel (scrollable)
    Inner panel (not scrollable, resize it whenever you add a control)
        User Defined Control 1
        User Defined Control 2
        User Defined Control 3
        User Defined Control 4
        ...

这样,附加控件的位置将相对于它们的直接父级(非滚动面板)。

You could add an additional panel into the hierarchy:

Outer panel (scrollable)
    Inner panel (not scrollable, resize it whenever you add a control)
        User Defined Control 1
        User Defined Control 2
        User Defined Control 3
        User Defined Control 4
        ...

This way, your additional controls' locations would be relative to their direct parent, the non-scrolling panel.

两仪 2024-11-02 19:59:57

如果您添加多个控件,请尝试在添加控件时暂停面板的布局:

panel1.SuspendLayout();
// Add controls ...
panel1.ResumeLayout();

这在用户可以动态更改现有控件的可见性的类似情况下对我有所帮助。

If you add several controls, try to suspend the layout of the panel while adding the controls:

panel1.SuspendLayout();
// Add controls ...
panel1.ResumeLayout();

This helped me in a similar situation where the user could change dynamically the visibility of existing controls.

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