Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Winforms FlowLayoutPanel - Strange height issue

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
Avatar of magicdlf
magicdlf

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;
        }
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.
Avatar of Unimatrix_001

ASKER

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

Thanks,
Uni
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;
        }
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
ASKER CERTIFIED SOLUTION
Avatar of magicdlf
magicdlf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hrm, as the comments say - a little tacky but it works... :)

Thanks,
Uni.
:)