views:

352

answers:

2

I am trying to get a very simple autosizing layout on a winform (C# .NET). I've tried TableLayoutPanels and FlowLayoutPanels but nothing works.

I have a usercontrol which is a container for other usercontrols which are created at runtime - I've called it StackPanel as I want it to list the child controls vertically. I've tried this using a FlowLayoutPanel, TableLayoutPanel and a Panel (with each control docked to the top).

The child usercontrol consists of a label and then any number of radiobuttons (or any other standard control - it doesn't matter).

When the child controls are created, the label text is set (if this is long it needs to wrap to a new line) and the radio buttons are added.

There seems to be no combination of docking/autosizing or manual size setting using the Resize events that can get everything to show without clipping and still resize with the form.

Thanks!

A: 

I've had a similar experience with both Panels.

FlowLayoutPanel:

You basically have to leave your child widgets anchored to the top left. Don't dock them. Also don't anchor them to the right. Label auto-sizing will only work in this configuration.

Then you can capture the resize event of the FlowLayoutPanel and manually adjust all the widths of your child components.

sechastain
A: 

@sechastain - did you have to use MeasureString and calculate the height of the labels?

Pete