0
Answer

Change nested Panel location during runtime

Mike Read

Mike Read

14y
3.6k
1
1st:  by Panel, I mean FlowLayoutPanel (though I have tried both)
Background:
I am dynamically adding a panel to another panel.  The parent panel is docked, the child is not.  I attempt to resize, and reposition the child panel when controls are added to it (ControlEventHandler).  The resize works properly, but I cannot change the location. I've tried setting the Left property of the panel, as well as creating a new Point location.  The panel just stays where it is.  I have already verified that my if statement is not excluding the code from running.

         private void ResizePanel(object oPanel, ControlEventArgs e)
        {
            FlowLayoutPanel pnl = (FlowLayoutPanel)oPanel;
            int w = pnl.Width;
            if (pnl.Parent.Width > pnl.Width)  // make sure we don't adjust position if it fills entire space
            {
                pnl.Left = (pnl.Parent.Width - pnl.Width) / 2;
            }
            pnl.Width = ((pnl.Height - 3) * pnl.Controls.Count) + 5;
        }


##################  UPDATE  ##################
since the reply function isn't working for me in Chrome, I'll just edit my original post...

Found the issue:
child controls in a panel have to be placed by adjusting their margin...Not really sure that makes perfect sense, but it works.  I just set the left margin to what I want, and set the remaining margins to the existing values.