Solved

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

Posted on 2004-10-07
4
513 Views
Last Modified: 2010-08-05
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
Comment
Question by:JElster
  • 2
4 Comments
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
Use the OnLayout event to calculate and set the size of the inner controls.
0
 
LVL 1

Author Comment

by:JElster
Comment Utility
How? Thanks
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 125 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now