Search code examples
c#winformsdockinglayout-anchor

How can I make left and right panels resize and center narrow panel stay centered with WinForms?


Mock-up

I am creating a WinForms application which is supposed to have three main vertical panels. The middle one (B) is narrow and centered. It is not to change in width, only in height as the form is resized, and it is always to stay in the horizontal center. The left (A) and right (C) panels are to resize to fill the rest of the available space.

I have tried the various docking and resizing options. But I haven't yet found the combination that will allow the left and right panels to fill the spaces on either side of the middle panel (which is to remain the same width.)

I'm still hoping that there is something that I'm missing, otherwise I will have to go the route of manual calculating the sizes and locations of the panels on the resizing event.

Even if I manually get B to remain in the center, I don't know of a way to get left and right to resize automatically without either covering or going behind the center panel.


Solution

  • Add the TableLayoutPanel to your Form and set its Dock property to Fill. Now edit the Rows/Columns. Remove the second row so there is only one row. Setup the Columns so they look like this:

    enter image description here

    Change the Absolute value of 50 to whatever width you want the middle to be. The two Percent values can be anything, as long as they are the same number.

    Now add the three Panels to each column and set the Dock property of each Panel to Fill.

    Done.

    Here's the result in action:

    enter image description here