Easiest way for controls to share the size of parent

M

Martin

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
 
M

Mick Doherty

there's not a lot of nasty calculations.

groupbox1.Height = panel.Height / 2;

the rest is down to docking.
Dock groupbox1 to Top and groupbox2 to Fill.
 
S

Shortgrey

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));
}
}
 
S

Steve McLellan

Take a look at the OnLayout event - you can iterate through the controls in
the Panel and make sure their heights are all the appropriate fraction of
the panel itself.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top