C#: How to get a user control to properly auto size itself
Asked Answered
N

5

24

I have a UserControl which consists of a Label (Top), a FlowLayoutPanel (Fill, TopDown flow and no wrap) and a Panel (Bottom). The user control creates a number of controls, based on a list of stuff it gets, and adds them to the FlowLayoutPanel.

How can I get this UserControl to properly resize itself so that the FlowLayoutPanel does not have any scroll bars? I have tried to use various combinations of AutoSize and AutoSizeMode on the FlowLayoutPanel and the UserControl itself, but I can't seem to get it working. Either I end up with something that doesn't resize itself at all, or it doesn't become big enough or it is squished down to almost nothing.

Neurasthenia answered 31/8, 2009 at 7:24 Comment(0)
N
13

Thanks for all the suggestions. The solution this time seemed to set AutoSize to true for both the FlowLayoutPanel and the UserControl itself.

Now, how to get the form which will contain this UserControl as well as some other controls, that I can't quite figure out at the moment, but I guess that should be a separate question...

Neurasthenia answered 31/8, 2009 at 11:26 Comment(1)
Here is a link to an answer for how to get the UserControl loaded on to a form. Basically either add your assembly to Toolbox or rebuild solution.Attorn
F
5

You can use the Anchor- and Dock property of the UserControl to set options so that the edges of your control gets "glued" to some other parts of your UI. When the UI gets resized, your control will follow along!

If you use anchors and dock on all controls in your user control and set them to dock the edges of the control, the controls will resize with the UserControl and you can now set anchors/dock to the UserControl also.

Fallacious answered 31/8, 2009 at 7:37 Comment(1)
That I know, but I don't want the control to resize according to their parent. I want it to grow or shrink so that it is just big enough to cover the contents of the FlowLayoutPanel without it getting scroll bars or clipping any contents.Neurasthenia
P
2

Wrapping the FlowLayoutPanel in a TableLayoutPanel will allow for proper autosize behavoir.

Your UserControl should look like this:

UserControl
    TableLayoutPanel (Dock-Fill)
        Row1 : Label
        Row2 : FlowLayoutPanel (Panel:Dock-Fill AND AutoSize, Row:AutoSize)
        Row3 : Panel

Again, when using that UserControl, it is possible you will need to wrap it in a TableLayoutPanel using an AutoSize row or column.

Watch for SplitContainers since they often throw autosize behaviors out of balance.

Pickings answered 31/8, 2009 at 8:2 Comment(0)
H
0

I found that the best way to solve this was to use some code in the ControlAdded event on the flowlayoutpanel to adjust the height of the panel (the height because you were using the TopDown flow) to be:

e.Control.Location.Y + e.Control.Height + some margin

and to set the AutoSize property on the usercontrol itself to True.

You would also need to do some of docking and anchoring suggestions as per Mickel's answer.

This functionality could be encapsulated in to your own control inherited from FlowLayoutPanel and overriding the ControlAdded method.

Hemorrhoid answered 31/8, 2009 at 7:58 Comment(0)
S
0

On adding a control to the flowlayout you can resize the already added controls to the required size without getting the scroll bars for flow layout panel.l

Steersman answered 31/8, 2009 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.