theLuckyOne
theLuckyOne

Reputation: 272

Auto-resize multiple Forms rendered on Panel

I have this Form with Panels. This Form has three Panels.
One Panel is collapsible and acts as a sidebar, the other one sits at the top and is there for showing title, the last one is the placeholder for the Forms being opened by clicking on one of the items being catered in Panel one.

Now what I want to do is resize (grow and shrink) the size (width only) of the placeholder Panel and the Form that is opened on the Panel according to the state of Panel one, which could either be expanded or collapsed. The dock is not working.

enter image description here

Upvotes: 2

Views: 1129

Answers (1)

Jimi
Jimi

Reputation: 32248

After some clarifications, it appears that the desidered layout and behaviour of the described Form is similar to this sample disposition:

A WinForms Form is embedded in another Form, and placed inside a Panel.
This Guest Form is stripped of its TopLevel coat-of-arms and parented to central Panel, as shown in this graphic example:

enter image description here

How do you dock these Panels to get this layout:

The Green Panel stays on top of the Form.
The DarkGray Panel lays on the left hand side of the Form.
The Gray Panel occupies the remaining space.

  • Insert three Panels on a Form container.
  • The Green Panel needs to maintain its position, it will never change:
    • Right click → SendToBack (!important :).
    • Dock → Top.
  • The DarkGray Panel is positioned under the Green Panel, on the left side of the Form. It needs to resize itself when needed, but will never cover the Green Panel:
    • Dock → Left
  • The Gray Panel needs to occupy the remaining space. It needs to resize itself when needed, but it will never cover Green Panel or Dark Gray Panel:
    • Right click → BringToFront (!important)
    • Dock → Center

The highest priority when docking, is assigned to the element which has the lowest z-order in the stack: the Green Panel, here. The lowest priority is assigned to element with the highest z-order: the Gray Panel, which will then shrink and stretch among all other elements with higher priority (following the z-order).

How to embed the Form:

The easy part. It's a Form in our Project, no need to perform any magic to keep it alive when re-parented:
(This is just for 1 Form. With more Forms, you'll need something like a List<Control>:

//Define here the Form which will be embedded
[Your Form Class] EmbeddedForm;

private void button1_Click(object sender, EventArgs e)
{
    EmbeddedForm = new [Your Form Class]() {
        TopLevel = false,
        Parent = panContainer,
        Location = new Point(4, 4),
        Enabled = true
    };
    EmbeddedForm.Show();
}

private void buttonShrink_Click(object sender, EventArgs e)
{
    //Maybe insert a classic dotted mini-button to re-inflate the sidebar when needed
    panelSideBar.Width = 6;
}

private void panelContainer_Resize(object sender, EventArgs e)
{
    Rectangle rect = panelContainer.ClientRectangle;
    rect.Inflate(-3, -3);
    EmbeddedForm.Size = rect.Size;
}

If you allow your Container Panel to AutoScroll its content, the Resize event is not necessary.

Edit:
A PasteBin of the complete source code of the Form in sample graphics: Embedded Forms

Upvotes: 3

Related Questions