This simple program Docks two Panels to top edge. Without anchoring the result is a blue panel on top of the green panel (Y-axis not Z-axis). Normal and as expected.
Adding anchoring for both panels in all four directions I expected proportional resizing on Form resize.
This probably happens except the panels end up in the same Y position, so one panel obscures the other.
How to get two panels docked to the top edge resize proportionally when the containing Form resizes?
.
.
.
using System;
using System.Windows.Forms;
using System.Drawing;
public class OnlyBlueGreen : Form
{
Panel bluePanel;
Panel greenPanel;
public OnlyBlueGreen()
{
InitiateGraphics();
}
void InitiateGraphics()
{
// blue
bluePanel = new Panel();
bluePanel.BackColor = Color.LightBlue;
bluePanel.Width = this.Width;
bluePanel.Height = 25;
bluePanel.Dock = DockStyle.Top;
// green
greenPanel = new Panel();
greenPanel.BackColor = Color.LightGreen;
greenPanel.Width = this.Width;
greenPanel.Height = bluePanel.Height;
greenPanel.Dock = DockStyle.Top;
this.Controls.Add(greenPanel);
this.Controls.Add(bluePanel);
bluePanel.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
greenPanel.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
}
public static void Main()
{
Application.Run(new OnlyBlueGreen());
}
}
Loading
pagefault pagefaultPosted May 17, 2012, 8:41 AM
Specifically if using "bluePanel.Top = greenPanel.Top + greenPanel.Height;" only one of the panels resizes, the other maintains it's distans to bottom edge without resizing.
What I wanted was two panels docked to top edge resizing proportionally using *only* Dock and Anchor properties. Now I think that can't be done without using an resize eventhandler).
Unless one can make each grow a half if two panels, grow a third if three panels ... but for that an resize eventhandler is needed.
Kunal VaishyaPosted May 17, 2012, 12:48 AM
Set On
bluePanel.Top = greenPanel.Top + greenPanel.Height;
On Form _Resize Event
let me know is it ok