This article describes
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.
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
- initializing it
- giving it controls to manage
- 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
Controls to be managed by
are added as
s, each of which refer
to a control and how it should be repositioned and/or resized.
You can add, remove and modify
at any time during the execution of your program.
(m_editTitle, 0, 0, 100, 66)); ...
Finally, your form's
instructs the layout manager to align its items.
private void LayoutManagerDemoFrm_SizeChanged
(object sender, EventArgs e)
How it works
works by saving the container's orginal
), 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
LayoutManagerItem encapsulates a reference to the
control to be repositioned or resized, and four boolean properties
that describe the adjustment to be performed.
are stored in
- 3 Sep 2006
Added ability to control percentage of change in dimensions.
-- Eddie Zhou, Ravi Bhavnani
- 1 Jul 2006
-- Ravi Bhavnani