Search code examples
c#user-interfaceresizeresolutiontablelayoutpanel

TableLayoutPanel stretch to fill parent panel in form


I'm making a WinForms GUI that should be resolution independent (ie. all controls stretch/shrink while staying in place, regardless of the user's resolution).

My issue is: For some reason, all my controls resize perfectly as long as the form is being shrunk, not stretched.

If the user resizes the window to make it smaller (once I'm finished, it'll be locked to maximized, but for testing purposes I've left it resizable), all of the controls in the window follow suit. If the user stretches the window beyond it's initial size, the controls stay in place, which in turn means they are no longer in the right position, since the window is now larger. They resize just fine from very small, up to their initial size, and then they just stop; while the window continues to grow.

My layout works as follows:

Main form
   --> 1x3 TableLayoutPanel
      --> 1x1 TableLayoutPanel in center row of parent panel
         --> User control that populates parent 1x1 TableLayoutPanel on button click

            User Control
               --> 1x1 TableLayoutPanel
                  --> Multiple TableLayoutPanel's laying out multiple controls inside parent 1x1 TableLayoutPanel

All of my panels are set to anchor TOP/BOTTOM/LEFT/RIGHT

I've tried different combinations of autosize, grow style, dock and anchor for each main panel, but can't seem to get my main panels (ie. panel containing the user control) to resize larger than their initial size.

I hope I've described my issue well enough to understand, but basically; my project layout consists of many TableLayoutPanels inside of eachother for each custom control, and that custom control populates a 1x1 TableLayoutPanel inside my main form which acts as the "Main" panel for my GUI. The user then selects which user control to display on the main form with a button located in the top row of the parent 1x3 TableLayoutPanel.

Let me know if I need to explain this any better in order for you to answer


EDIT: I've discovered if I set the AutoScaleMode of the User Control to Font it will resize exactly how I want it to. The only issue with this solution is, when the control gets loaded into the form, it's way too big, and some of it is cut off by the edges of my main form. Once I manually resize the form (even a tiny bit), the control fixes itself to the proper size within the form.

So I feel like I'm close, but not quite there, as with the finished product, the user will have no control over resizing the window, it will be forced to maximize.

EDIT 2: I've tried adding this to the button event which displays the user control within the panel:

            MyUserControl MainPanelControls = new MyUserControl();
            MainPanel.SuspendLayout();
            MainPanel.Controls.Clear();
            MainPanel.Controls.Add(MainPanelControls); 
            MainPanel.Dock = DockStyle.Fill;  //This seems to have no effect               
            MainPanel.ResumeLayout(false);

Solution

  • It turns out the only problem was for some reason I had MainPanel.ResumeLayout(false); instead of true. I'm not sure why I had that set to false, but everything is working fine now.

    So to answer my own question:

    To make the user control scale properly, I had to set it's AutoScaleMode to Font and then on my button event that adds the user control to the main TableLayoutPanel I had to change MainPanel.ResumeLayout(false) to MainPanel.ResumeLayout()

    private void Button1_Click(object sender, EventArgs e)
           {
            MyUserControl MainPanelControls = new MyUserControl();
            MainPanel.SuspendLayout();
            MainPanel.Controls.Clear();
            MainPanel.Controls.Add(MainPanelControls);             
            MainPanel.ResumeLayout();
           }