0
votes

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.

2
Do you have some code to show what approach you have taken to solve this problem? - Steve
. I don't think just a few properties or containers will suffice. You will have to write code for that. Use the VisibleChanged event! - TaW
@Steve no code yet but I've tried dock and anchor properties but no luck :-( - kid byte
@Taw I'll try to look on that too. - kid byte
Can you give me more information about your problem? I use splitContainer and have no problem - Ehsan Mohammadi

2 Answers

0
votes

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.

1
votes

Just an addt'l info, It seems tablelayout has a potential solution to my problem but I like @ANaik solution more.

            if (myPanel.Visible)
            {
                myPanel.Visible = false;

                tableLayoutPanel1.SetCellPosition(myPanel, new 
                TableLayoutPanelCellPosition(0, 1));
                tableLayoutPanel1.SetColumnSpan(myPanel, 2);

            }
            else
            {
                myPanel.Visible = true;

                tableLayoutPanel1.SetCellPosition(myPanel, new TableLayoutPanelCellPosition(1, 1));
                tableLayoutPanel1.SetColumnSpan(myPanel, 1);
            }