Auto-Resize Multiple Forms Rendered on Panel

Auto-resize multiple Forms rendered on Panel

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:

Sample Image

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

resize panel width according to form maximizing in c#

You need to use Anchor property of the panel during Design time.
You can find it in PropertyGrid of panel control.

How to resize form in panel ( C# )

you should use Anchors on the Panel Too, Set them to RIGHT | LEFT | TOP | BOTTOM for a all direction resizing with the container..

if the pannel is nested in another panel, notice that the container should have the same Anchors too...

splitmain.Panel2.Anchor = Anchor.Left | Anchor.Right | Anchor.Top | Anchor.Bottom;

play with Anchors till you get the hang of it!

you can watch this movie to get little bit more familiar:
http://www.youtube.com/watch?v=oO_zbWVklS8

Controls in panel are hidden until I resize the window

@Yas,

This was a rather intriguing problem--didn't quite grasp it until loading/running your code. I happen to agree with Jimi--that you're probably running up against an internal limitation, however bizarre that limitation appears to be. As you pointed out, tweaking the height/width of the form shouldn't overcome a limitation. So whatever's going on internally, it's a bit odd and unexpected.

But I have a solution for you: It's admittedly a kludge but sometimes that's our only option. If your project specs require a form like the one you presented--a form that ends up with a height value well over 100,000, then this solution should fit the bill.

Add the following code to the end of your Form1 initialization code, shown in your original post (see below). What I've done is programmatically "tweaked" the height of the form ever so slightly--repeatedly--until all of the panels have been fully rendered. I think this is going to be your best bet.

For the record, I had spent quite a bit of time on this. I had tried various combinations of Refresh() on various controls, DoEvents(), etc., to no avail. I even tried not using docking and autosizing, in favor of setting top and heights manually (that didn't work either). Thus, I think the code below is really the only way to fix this, aside from a fix from Microsoft.

** For the code below to work, add this line immediately after your code's InitializeComponent(); line:

this.Show();

Then add this code after your code's closing brace:

     // fix the panContents panel
long currentMaximum = 0;
while (true)
{
var preserveHeight = this.Height;
// tweak the height
this.Height += 1;
// set it back the height
this.Height = preserveHeight;

// test to see if the scroll bar max remains at its previous value
// if it does, we're done
if (currentMaximum == panContents.VerticalScroll.Maximum)
break;

// otherwise, preserve the current maximum scroll bar position
currentMaximum = panContents.VerticalScroll.Maximum;

// now scroll to the bottom of the panel
panContents.VerticalScroll.Value = panContents.VerticalScroll.Maximum;
// necessary step to ensure scroll bar value is set
panContents.PerformLayout();
}

// scroll to the top
panContents.VerticalScroll.Value = 0;
// necessary step to ensure scroll bar value is set
panContents.PerformLayout();

How do I make multiple controls in a Windows Form automatically resize with the window?

Dock is pretty easy to use, but I recommend using the Anchor properties instead. Resize your form to a reasonable size in the Designer. Then, place your controls to look the way you want. Then, decide which controls should resize with the form and set the Anchor property as follows:

  • If you want the control to resize with the form in width, set the Right anchor.
  • If you want to resize height, set the Bottom anchor.
  • If you want the control to stay right when the form resizes, unset the Left anchor.
  • If you want the control to stay bottom when the form resizes, unset the Top anchor.

The problem I have with Docks is that they sometimes act funny when controls are not declared in a specific order, and to get the effect you want, sometimes you have to create extraneous panels just to hold controls.

How to resize correctly a panel automatically when another panel is dinamically moving java swing

After using the recommendation of @JBNizet, i have used the BorderLayout https://docs.oracle.com/javase/8/docs/api/java/awt/BorderLayout.html to fit according to my specifications. And working fine now, The button is placed in north direction, the jPanel2 in west direction and jPanel1 in the center.



Related Topics



Leave a reply



Submit