Xplouder
Xplouder

Reputation: 148

Hide and reShow Panels/components in Windows Forms

Initial Panels Position - image

Hi, im trying to make one button action that do the following behaviour:

1) when i first click the button1 the panel1 will dissapear and the panel2 will get the size of Parent container.

2) If i reclick the button the panel1 will appear again and the panel2 will rezise.

Then problem is on step 2, when i reclick the button1 the panel2 dont resize well and get, somehow fixed values (check: problem - image).

i was trying something like this:

private void button1_Click(object sender, EventArgs e){

        if (panel1.Visible){
            panel1.Visible = false;
            panel1.Enabled = false;         
            panel2.Dock = DockStyle.Fill;
        }
        else{
            panel1.Visible = true;
            panel1.Enabled = true;
            panel2.Dock = DockStyle.None;
            panel2.Anchor = ((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right;
        }

    }

PS: Check the image link, might help to understand the problem. Also the panel2 in my project is a WebBrowser component.

Thank you.

Upvotes: 3

Views: 2986

Answers (3)

Dhaval Joshi
Dhaval Joshi

Reputation: 54

use the splitcontainer control and add both panel control in its panle1 and panel2.

by clicking on button set splitcontainer1.panle1.visible = true and splitcontainer1.panle2.visible=false and next click splitcontainer1.panle1.visible = false and splitcontainer1.panle2.visible=true

Upvotes: 0

LarsTech
LarsTech

Reputation: 81620

Actually, the control you should probably be using is the SplitContainer control.

You can use Panel1 of the SplitContainer as your first Panel as-is, and in Panel2 of the SplitContainer, you would Dock-Fill your WebBrowser control.

If the panels aren't supposed to be resized by the user, then set

splitContainer1.IsSplitterFixed = true;

To hide the first Panel, all you would call is:

splitContainer1.Panel1Collapsed = true;

You obviously set it to false to bring it back.

Upvotes: 1

Nuru Salihu
Nuru Salihu

Reputation: 4928

Before you run, copy the location of the panel1 and repost it to the same location. Example

  if (panel1.Visible){
            panel1.Visible = false;
            panel1.Enabled = false;         
            panel2.Dock = DockStyle.Fill;
        }
        else{
            panel1.Visible = true;
            panel1.Enabled = true;
            panel2.Dock = DockStyle.None;
            panel2.Location = new Point(15, 15);
            panel2.Height= //Original height before you run.
            panel2.Anchor = ((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right;
        }

Note : new Point will hold the location before you run. Also try add the same height and width . I think that might work. I am thinking this change in location is due to the dock property used and just undocking might not be sufficient.

Update on TableLayoutPanel

When i have complex cntrols that requires changes dynamically. In that case, i suggest using TAbleLayoutPanel. You can put your panels in a table layout, and dock the table in where ever you want. You caan dock the panels inside the table also. Then you can hidde, dock and undock and your panel will be in a fix grid, because its in a a table. Check TableLayoutPanel Here or better a TableLAyoutPanel on youtube. Hope this help.

Upvotes: 0

Related Questions