Click here to Skip to main content
Licence CPOL
First Posted 30 Jun 2006
Views 53,552
Bookmarked 56 times

Drop-dead easy layout management

By , | 3 Sep 2006 | Article
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.
  // 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.
  // 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.
  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)

About the Authors

Ravi Bhavnani



Canada Canada

Member

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, CHI 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 2 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.

Eddie Zhou

Web Developer

United States United States

Member

Eddie Zhou

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThanks for sharing. PinmemberMember 11939696:55 29 Aug '11  
AnswerRe: Thanks for sharing. PinmemberRavi Bhavnani1:28 31 Aug '11  
Generalresize issue PinmemberAshar Shah23:53 13 Dec '09  
GeneralRe: resize issue PinmemberRavi Bhavnani2:57 14 Dec '09  
GeneralCommercial Layout Manager Pinmembervkhaitan19:38 6 Sep '08  
QuestionWhat about TableLayoutPanel? PinmemberRiz Thon2:32 4 Mar '08  
GeneralRe: What about TableLayoutPanel? PinmemberRavi Bhavnani2:44 4 Mar '08  
QuestionFlicker with tab control PinmemberDavid McMinn5:23 13 Feb '08  
GeneralRe: Flicker with tab control Pinmemberlobotomoy8:49 4 Apr '08  
GeneralRe: Flicker with tab control PinmemberDavid McMinn6:45 9 Apr '08  
GeneralPositioning controls with respect to another controls PinmemberV-I-C-K-Y21:16 31 Jan '07  
GeneralA Small bug maybe Pinmemberhnkaraca7920:35 3 Sep '06  
AnswerRe: A Small bug maybe PinmemberRavi Bhavnani3:00 4 Sep '06  
GeneralRe: A Small bug maybe Pinmemberhnkaraca794:24 4 Sep '06  
GeneralRe: A Small bug maybe PinmemberRavi Bhavnani4:30 4 Sep '06  
GeneralRe: A Small bug maybe Pinmemberhnkaraca7920:16 4 Sep '06  
GeneralRe: A Small bug maybe PinmemberEddie Zhou4:35 5 Sep '06  
AnswerRe: A Small bug maybe - an easy solution? PinmemberGavin Jerman12:01 17 Sep '06  
GeneralRe: A Small bug maybe - an easy solution? PinmemberRavi Bhavnani12:07 17 Sep '06  
GeneralRe: A Small bug maybe - an easy solution? PinmemberGavin Jerman12:24 17 Sep '06  
Your welcome, and thanks for the excellent class in the first place Big Grin | :-D
 
Gavin
GeneralRe: A Small bug maybhttp://www.codeproject.com/script/images/news_general.gife - an easy solution? PinmemberEddie Zhou3:22 18 Sep '06  
GeneralThanks! Pinmembermattbrindley7:56 3 Sep '06  
GeneralRe: Thanks! PinmemberRavi Bhavnani8:07 3 Sep '06  
GeneralRe: Thanks! PinmemberClevedon_Peanut5:36 20 Jun '07  
GeneralRe: Thanks! PinmemberRavi Bhavnani8:53 20 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 3 Sep 2006
Article Copyright 2006 by Ravi Bhavnani, Eddie Zhou
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid