Winforms FlowLayoutPanel - Strange height issue

Unimatrix_001
Unimatrix_001 used Ask the Experts™
on
Hello,

Please see the project in the zip file. You'll notice that when you run it and click the button a new button is added to a flow panel. If you do this a few times until the newly placed button is off the panel display then look at the labels which give the heights of two panels, you'll see that the inner panel has a smaller height than the parent panel - despite the child panel clearly having a larger area?

Could somebody please explain what is happening here?

Thanks,
Uni
SampleProject.zip
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Put that into a timer, you will find out.
        private void timer1_Tick(object sender, EventArgs e)
        {
            //Update the labels.
            label1.Text = "Panel 1 Height   : " + panel1.Size.Height;
            label2.Text = "Flow Panel Height: " + flowLayoutPanel1.Size.Height;
        }

Commented:
From MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.layout.aspx
 Remarks

The Layout event occurs when child controls are added or removed, when the bounds of the control changes, and when other changes occur that can affect the layout of the control. The layout event can be suppressed using the SuspendLayout and ResumeLayout methods. Suspending layout enables you to perform multiple actions on a control without having to perform a layout for each change. For example, if you resize and move a control, each operation would raise a Layout event.

So this event will be triggered before a control's layout is going to change. In your code, you always display the inner height that before the adjusting.

Author

Commented:
What event would I have to handle if I wanted to be informed of things AFTER the layout has changed?

Thanks,
Uni
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Commented:
For example.
        private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
        {
            //Update the labels.
            label1.Text = "Panel 1 Height   : " + panel1.Size.Height;
            label2.Text = "Flow Panel Height: " + flowLayoutPanel1.Size.Height;
        }

Author

Commented:
Yes like that, but I cannot rely on the ControlAddded event, as this is for a custom component, and I cannot rely on controls to be added all the time - for example only one may be added (as is this case of a flow layout panel) and I'd have to rely something else.

Thanks,
Uni

Author

Commented:
Hrm, as the comments say - a little tacky but it works... :)

Thanks,
Uni.

Author

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial