Hi,
this is what you can do to proportionally resize all controls in a form
private Hashtable hash = new Hashtable();
private Point OrgSize ;
private void InitOrgRect()
{
OrgSize = new Point( ClientRectangle.Width,ClientRectangle.Height) ;
foreach(Control cl in Controls)
{
Rectangle r = new Rectangle(
cl.Location.X,cl.Location.Y,cl.Width,cl.Height );
hash.Add(cl,r);
}
}
protected override void OnSizeChanged(EventArgs e)
{
if( hash.Count == 0 )
return ;
int widthRatio = ClientRectangle.Width * 100/ OrgSize.X ;
int heightRatio = ClientRectangle.Height * 100/ OrgSize.Y;
foreach (Control ctl in Controls)
{
Rectangle r = (Rectangle)hash[ctl];
ctl.Width = r.Width * widthRatio / 100;
ctl.Height = r.Height * heightRatio / 100;
ctl.Location = new Point((r.X * widthRatio / 100),(r.Y * heightRatio
/ 100));
}
}
Martin wrote:
Maybe I'm tired, but I can't figure out a simple way for two controls to
share the size of the parent control. e.g when the parent's height becomes
NewHeight the first control's new height becomes 0.5*NewHeight and the
second control get the rest i.e 0.5*NewHeight.
A picture says more than 1000 words they say so lets see if there is some
truth in that
I have a Panel with two GroupBoxes
______________________________
| GroupBox1 |
| |
|______________________________|
| GroupBox2 |
| |
|______________________________|
Decreasing the height of the panel should give this result
______________________________
| GroupBox1 |
|______________________________|
| GroupBox2 |
|______________________________|
Surely there's an easy way to achieve this without having to override OnSize
and doing lots
of nasty calculations in there?
I'm starting to really miss the LayoutManagers I have in Java.
Martin