Search code examples
c#winformslayoutpanel

Form Panels Resizing


How to resize panels inside a form accordingly?

My form has 3 panels

Form with 3 panels

If I set panel2.visible to false, panel3 occupies the space.

Panel3 to panel2

If I set panel1.visible to false, panel3 also occupies the rest.

panel3 to panel1 and 2

Going back to original form - all 3 panels are present.

If I set panel1.visible to false, panel2 and 3 occupies the space of panel1

panel2 and 3 to panel1

Once panels 1 or 2 are visible again, panel 3 will go back the way it was.


Solution

  • It seems that as per your cases in question. Panel1 will be always on top even when panel 2 and 3 are hidden. In same way panel 2 will be always left aligned. Panel 3 will be always occupy available space .

    Please set dock properties on panel control.

    Panel1.Dock = top
    Panel2.Dock = left
    Panel3.Dock = fill
    

    One more thing when controls are hidden make their sizes as 0. And When visible set sizes to default.

    I hope this helps.