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.

How to use LayoutManager

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.

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.

Share

About the Authors

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 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.