开发者

Adding to a Scrollable Panel with Location

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?


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜