Click here to Skip to main content
15,888,579 members
Articles / Programming Languages / C#
Article

Drop-dead easy layout management

,
Rate me:
Please Sign up or sign in to vote.
4.33/5 (15 votes)
3 Sep 2006CPOL2 min read 100.4K   2.2K   63   29
Flexible control repositioning and resizing with an easy-to-use layout manager.

Introduction

This article describes LayoutManager, a lightweight class that lets you easily reposition and resize controls when the size of their container changes.  Although the .NET framework provides out-of-the-box support for anchoring (and consequently automated layout management), we prefer to use a class that is absurdly flexible and something we understand intuitively.  LayoutManager replaces terminology such as "table layout", "grid-bag layout", "rubber layout", etc. with four simple adjustments on a control's edge:
  • adjust left percentage
  • adjust top percentage
  • adjust right percentage
  • adjust bottom percentage
The ability to use any (or all) of these adjustments in response to a resize operation allows you to implement any type of reposition/resize logic that can be freely changed at run time.

LayoutManager in action

How to use LayoutManager

You use LayoutManager by:
  1. initializing it
  2. giving it controls to manage
  3. calling its alignItems() method within your container's SizeChanged event handler
Steps 1 and 2 are usually performed in your form's constructor but can just as well be added to the Load event handler.
C#
// Step 1: Initialize the layout manager
m_layoutMgr.init (this);
Controls to be managed by LayoutManager are added as instances of LayoutManagerItems, each of which refer to a control and how it should be repositioned and/or resized.  You can add, remove and modify LayoutManagerItems at any time during the execution of your program.
C#
// Step 2: Add controls to the layout manager
m_layoutMgr.Items.Add
  (new LayoutManagerItem
    (m_editTitle,         // the control
     0,                   // don't change L edge
     0,                   // don't change top edge
     100,                 // grow width by 100% of change
     66));                // grow height by 66% of change
...
Finally, your form's SizeChanged event handler instructs the layout manager to align its items.
C#
private void LayoutManagerDemoFrm_SizeChanged
  (object sender, EventArgs e)
{
  // Step 3: Instruct the layout manager to align items
  m_layoutMgr.alignItems();
}

How it works

LayoutManager works by saving the container's orginal size (within init()), computing the change in the container's width and height and applying adjustments to each item under its control according to the item's adjustment rules (within alignItems()).

LayoutManagerItem encapsulates a reference to the control to be repositioned or resized, and four boolean properties that describe the adjustment to be performed.  LayoutManagerItems are stored in LayoutManager's Items property.

LayoutManager classes

Revision history

  • 3 Sep 2006
    Added ability to control percentage of change in dimensions.
    -- Eddie Zhou, Ravi Bhavnani
  • 1 Jul 2006
    Initial version.
    -- Ravi Bhavnani

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Canada Canada
Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, marketing automation software, EDA tools, a platform to help people find, analyze and understand information, trading software for institutional investors and advanced data visualization solutions. He currently works for a company that provides enterprise workforce management solutions to large clients.

His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, HCI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and was a Microsoft MVP (C++ and C# in 2006 and 2007). He is also the co-inventor of 3 patents on software security and generating data visualization dashboards. His claim to fame is that he crafted CodeProject's "joke" forum post icon.

Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.

Written By
Web Developer
United States United States
Eddie Zhou

Comments and Discussions

 
QuestionThanks for sharing. Pin
Member 119396929-Aug-11 6:55
Member 119396929-Aug-11 6:55 
AnswerRe: Thanks for sharing. Pin
Ravi Bhavnani31-Aug-11 1:28
professionalRavi Bhavnani31-Aug-11 1:28 
Generalresize issue Pin
Ashar Shah13-Dec-09 23:53
Ashar Shah13-Dec-09 23:53 
GeneralRe: resize issue Pin
Ravi Bhavnani14-Dec-09 2:57
professionalRavi Bhavnani14-Dec-09 2:57 
GeneralCommercial Layout Manager Pin
vkhaitan6-Sep-08 19:38
vkhaitan6-Sep-08 19:38 
QuestionWhat about TableLayoutPanel? Pin
Riz Thon4-Mar-08 2:32
Riz Thon4-Mar-08 2:32 
GeneralRe: What about TableLayoutPanel? Pin
Ravi Bhavnani4-Mar-08 2:44
professionalRavi Bhavnani4-Mar-08 2:44 
QuestionFlicker with tab control Pin
David McMinn13-Feb-08 5:23
David McMinn13-Feb-08 5:23 
GeneralRe: Flicker with tab control Pin
lobotomoy4-Apr-08 8:49
lobotomoy4-Apr-08 8:49 
GeneralRe: Flicker with tab control Pin
David McMinn9-Apr-08 6:45
David McMinn9-Apr-08 6:45 
GeneralPositioning controls with respect to another controls Pin
V-I-C-K-Y31-Jan-07 21:16
V-I-C-K-Y31-Jan-07 21:16 
GeneralA Small bug maybe Pin
hnkaraca793-Sep-06 20:35
hnkaraca793-Sep-06 20:35 
AnswerRe: A Small bug maybe Pin
Ravi Bhavnani4-Sep-06 3:00
professionalRavi Bhavnani4-Sep-06 3:00 
GeneralRe: A Small bug maybe Pin
hnkaraca794-Sep-06 4:24
hnkaraca794-Sep-06 4:24 
GeneralRe: A Small bug maybe Pin
Ravi Bhavnani4-Sep-06 4:30
professionalRavi Bhavnani4-Sep-06 4:30 
GeneralRe: A Small bug maybe Pin
hnkaraca794-Sep-06 20:16
hnkaraca794-Sep-06 20:16 
GeneralRe: A Small bug maybe Pin
Eddie Zhou5-Sep-06 4:35
Eddie Zhou5-Sep-06 4:35 
AnswerRe: A Small bug maybe - an easy solution? Pin
Gavin Jerman17-Sep-06 12:01
Gavin Jerman17-Sep-06 12:01 
As you say, the problem results from the accumulated rounding errors due to calculating the new position by applying the percentage change to the current position - each rounding error is carried forward to the next move.

The simple solution is to apply the percentage change to the initial position and to calculate the delta from the original position. This way there is only ever one rounding error applied to a control's position.

Here are the code changes that implement my solution:

LayoutManager.alignItems() - don't set m_nWidth and m_nHeight with the container's new size i.e.
<br />
  public class LayoutManager<br />
  {<br />
    #region Properties<br />
<br />
    /// <summary><br />
    /// Gets the LayoutManager's collection of items.<br />
    /// </summary><br />
    public ArrayList Items<br />
    {<br />
      get { return m_items; }<br />
    }<br />
<br />
    #endregion<br />
<br />
    #region Operations<br />
<br />
    /// <summary><br />
    /// Initializes the layout manager.<br />
    /// </summary><br />
    /// <param name="container">Container control.</param><br />
    public void init(Control container)<br />
    {<br />
      // Cache the operating context and initial dimensions<br />
      m_container = container;<br />
      m_nWidth = container.Width;<br />
      m_nHeight = container.Height;<br />
    }<br />
<br />
    /// <summary><br />
    /// Aligns the items under the layout manager's control.<br />
    /// </summary><br />
    public void alignItems()<br />
    {<br />
      // Ignore action if container has been minimized<br />
      if (m_container is Form)<br />
      {<br />
        Form f = m_container as Form;<br />
<br />
        if (f.WindowState == FormWindowState.Minimized)<br />
          return;<br />
      }<br />
<br />
      // Compute change in dimensions<br />
      int nDeltaWidth = m_container.Width - m_nWidth;<br />
      int nDeltaHeight = m_container.Height - m_nHeight;<br />
<br />
      // Align controls<br />
      foreach (LayoutManagerItem lim in m_items)<br />
      {<br />
        lim.align(nDeltaWidth, nDeltaHeight);<br />
      }<br />
    }<br />
<br />
    #endregion<br />
<br />
    #region Fields<br />
<br />
    /// <summary>Layout manager items to be aligned.</summary><br />
    ArrayList m_items = new ArrayList();<br />
<br />
    /// <summary>The container control.</summary><br />
    Control m_container = null;<br />
<br />
    /// <summary>Container's initial client width.</summary><br />
    int m_nWidth = 0;<br />
<br />
    /// <summary>Container's initial client height.</summary><br />
    int m_nHeight = 0;<br />
<br />
    #endregion<br />
  }<br />

Note - I also moved the align method to the LayoutManagerItem class as it seemed to make more sense to me to have it there.

Added 4 new fields to LayoutManagerItem to store the initial position values, set these in the constructor and use them to calculate the control's new size/position in the align() method i.e.
<br />
  public class LayoutManagerItem<br />
  {<br />
<br />
    #region Constructor<br />
<br />
    /// <summary><br />
    /// Constructs a LayoutManagerItem for a control.<br />
    /// </summary><br />
    /// <param name="ctrl">The control.</param><br />
    /// <param name="leftAdjustPct">Adjust control's left edge by this %age change in container's width.</param><br />
    /// <param name="topAdjustPct">Adjust control's top edge by this %age change in container's height.</param><br />
    /// <param name="rightAdjustPct">Adjust control's right edge by this %age change in container's width.</param><br />
    /// <param name="bottomAdjustPct">Adjust control's bottom edge by this %age change in container's height.</param><br />
    public LayoutManagerItem(Control ctrl, uint leftAdjustPct, uint topAdjustPct, uint rightAdjustPct, uint bottomAdjustPct)<br />
    {<br />
      // the control<br />
      m_ctrl = ctrl;<br />
<br />
      // Cache the control's initial coordinates<br />
      m_left = m_ctrl.Left;<br />
      m_top = m_ctrl.Top;<br />
      m_right = m_left + m_ctrl.Width;<br />
      m_bottom = m_top + m_ctrl.Height;<br />
<br />
      // Cache the control's adjustment percentages<br />
      m_leftAdjustPct = leftAdjustPct;<br />
      m_topAdjustPct = topAdjustPct;<br />
      m_rightAdjustPct = rightAdjustPct;<br />
      m_bottomAdjustPct = bottomAdjustPct;<br />
    }<br />
<br />
    #endregion<br />
<br />
    #region Operations<br />
<br />
    /// <summary><br />
    /// Repositions/resizes a LayoutManagerItem.<br />
    /// </summary><br />
    /// <param name="nDeltaW">Change in container's initial width.</param><br />
    /// <param name="nDeltaH">Change in container's initial height.</param><br />
    public void align(int nDeltaW, int nDeltaH)<br />
    {<br />
      // Calculate control's new coordinates using initial coordinates<br />
      int nLeft = m_left + (int)((nDeltaW * m_leftAdjustPct) / 100);<br />
      int nTop = m_top + (int)((nDeltaH * m_topAdjustPct) / 100);<br />
      int nRight = m_right + (int)((nDeltaW * m_rightAdjustPct) / 100);<br />
      int nBottom = m_bottom + (int)((nDeltaH * m_bottomAdjustPct) / 100);<br />
<br />
      // Set control's new coordinates<br />
      m_ctrl.Left = nLeft;<br />
      m_ctrl.Top = nTop;<br />
      m_ctrl.Width = nRight - nLeft;<br />
      m_ctrl.Height = nBottom - nTop;<br />
    }<br />
<br />
    #endregion<br />
<br />
    #region Fields<br />
<br />
    /// <summary>The control.</summary><br />
    Control m_ctrl = null;<br />
<br />
    /// <summary>Percentage by which control's left edge should be adjusted.</summary><br />
    uint m_leftAdjustPct = 0;<br />
<br />
    /// <summary>Percentage by which control's top edge should be adjusted.</summary><br />
    uint m_rightAdjustPct = 0;<br />
<br />
    /// <summary>Percentage by which control's right edge should be adjusted.</summary><br />
    uint m_topAdjustPct = 0;<br />
<br />
    /// <summary>Percentage by which control's bottom edge should be adjusted.</summary><br />
    uint m_bottomAdjustPct = 0;<br />
<br />
    /// <summary>Control's initial left position</summary><br />
    public int m_left = 0;<br />
<br />
    /// <summary>Control's initial top position</summary><br />
    public int m_top = 0;<br />
<br />
    /// <summary>Control's initial right position</summary><br />
    public int m_right = 0;<br />
<br />
    /// <summary>Control's initial bottom position</summary><br />
    public int m_bottom = 0;<br />
<br />
    #endregion<br />
  }<br />

These fixes seem to correct the size/position problem.

Gavin
GeneralRe: A Small bug maybe - an easy solution? Pin
Ravi Bhavnani17-Sep-06 12:07
professionalRavi Bhavnani17-Sep-06 12:07 
GeneralRe: A Small bug maybe - an easy solution? Pin
Gavin Jerman17-Sep-06 12:24
Gavin Jerman17-Sep-06 12:24 
GeneralRe: A Small bug maybhttp://www.codeproject.com/script/images/news_general.gife - an easy solution? Pin
Eddie Zhou18-Sep-06 3:22
Eddie Zhou18-Sep-06 3:22 
GeneralThanks! Pin
mattbrindley3-Sep-06 7:56
mattbrindley3-Sep-06 7:56 
GeneralRe: Thanks! Pin
Ravi Bhavnani3-Sep-06 8:07
professionalRavi Bhavnani3-Sep-06 8:07 
GeneralRe: Thanks! Pin
Pete Goodsall20-Jun-07 5:36
Pete Goodsall20-Jun-07 5:36 
GeneralRe: Thanks! Pin
Ravi Bhavnani20-Jun-07 8:53
professionalRavi Bhavnani20-Jun-07 8:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.