[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

WinForm: Resize a form and 'inner controls' at runtime.

I have user control that contains several panels.
I would like to set the control's (The Form) width and height programmatically at runtime, and have the inside controls resize accordingly... maybe by percentage
Any ideas?
Thanks in advance
0
JElster
Asked:
JElster
  • 2
1 Solution
 
ptmcompCommented:
Use the OnLayout event to calculate and set the size of the inner controls.
0
 
JElsterAuthor Commented:
How? Thanks
0
 
ptmcompCommented:
That's the code I used in my test project:

The form contained a Panel "pnlHost" which contained four controls.

            private void panel1_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
            {
                  if (e.AffectedControl != null)
                  {
                        Debug.WriteLine(e.AffectedControl.Name);
                  }
                  if (e.AffectedProperty != null)
                  {
                        Debug.WriteLine(e.AffectedProperty.ToString());
                  }
                  if ((e.AffectedControl == pnlHost) && (e.AffectedProperty == "Bounds"))
                  {
                        UpdateInnerControlBounds();
                  }
            }

            private void UpdateInnerControlBounds()
            {
                  pnlHost.Controls[0].Top = 0;
                  pnlHost.Controls[0].Left = 0;
                  pnlHost.Controls[0].Width = 100;
                  pnlHost.Controls[0].Height = (int)(pnlHost.Height * 0.3);

                  pnlHost.Controls[1].Top = 0;
                  pnlHost.Controls[1].Left = 100;
                  pnlHost.Controls[1].Width = pnlHost.Width - 100;
                  pnlHost.Controls[1].Height = pnlHost.Height - 200;

                  pnlHost.Controls[2].Top = pnlHost.Controls[0].Height;
                  pnlHost.Controls[2].Left = 0;
                  pnlHost.Controls[2].Width = 100;
                  pnlHost.Controls[2].Height = pnlHost.Height - pnlHost.Controls[0].Height;

                  pnlHost.Controls[3].Top = pnlHost.Controls[1].Height;
                  pnlHost.Controls[3].Left = 100;
                  pnlHost.Controls[3].Width = pnlHost.Width - 100;
                  pnlHost.Controls[3].Height = 200;      
            }
      }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now