Add to scrollable layout with location

Hi guys, I'm using Panel to store a list of controls (user defined). As I add panels, I set the location of the control based on Panel.Controls.Count before adding it to the panel.

 comRec.Location = new Point(comRec.Location.X, panel1.Controls.Count * 25); panel1.Controls.Add(comRec); 

Now it works beautifully and looks the way I want. However, as soon as we reach the limit in the window, AutoScroll resolves (what I want). Now, if the user had to scroll to the bottom of the Panel , this would eventually change the location of each control in the panel. Instead of my first comRec.Location (0,0), it is something like (0, -219). Now, when the user adds another comRec object, he creates a HUGE gap between the objects.

My question is what is the best way to account for location changes using the scroll bar and using my add system. I assume that you will have to do something by checking the value of the scrollbar and using it to determine the location.

Also is there a BEST way to display a list of controls? Should I use Panel ?

+4
source share
3 answers

Look at the FlowLayoutPanel , that's exactly what you are.

+5
source

You can add an additional panel to 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 ... 

Thus, your locations for additional controls will refer to their direct parent, and not to the scroll bar.

+1
source

If you have added multiple controls, try pausing the layout of the panel when adding controls:

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

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

+1
source

Source: https://habr.com/ru/post/1345255/


All Articles