Solved

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

Posted on 2004-10-07
4
514 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
ID: 12249584
Use the OnLayout event to calculate and set the size of the inner controls.
0
 
LVL 1

Author Comment

by:JElster
ID: 12249784
How? Thanks
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 125 total points
ID: 12262600
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 160
fomat Json objects 6 28
Achieve json result 2 65
compare between entities in entity framework 8 12
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

25 Experts available now in Live!

Get 1:1 Help Now