Layout Managers In C#

Project – 10.2 KB
Library Download + XML Documentation – 12.0 KB

Whenever a programmer sits down to design aGUI, he is faced with several problems unique to their design: visualappeal, scalability, resizability, ease of modification, etc. Layoutmanagers help to solve these problems by taking care of setting thesize and location of layoutable objects. Add the fact that layoutmanagers themselves are layoutable, and all kinds of layoutpossibilities open up. Java programmers are familiar with the conceptof the layout manager, however programmers coming to C# from otherprogramming backgrounds might need some getting used to them. If youare from a Java background, then you should read the section ondifferences between layout managers in Java and my implementation inC#. If you are unfamiliar with layout managers, then continue on andread the section on layout managers in general.

What is a Layout Manager?

A layout manager is anything which is able todecide the size and position of layoutable objects and which is itselflayoutable. They can be used in any situation, however they are mostoften used in Graphical User Interfaces. As anyone who has everprogrammed a GUI can attest, it tends to be a hassle at best. Some ofthe tools which have become available in recent years, most based uponBorland’s great C++ Builder series, have made GUI design easier, buteven with these sophisticated tools, GUIs are still a complicatedproblem. For those who want to simplify this problem or want moredirect control over layout, layout managers are the way to go. A layoutmanager gives a lot of power over design to the programmer, as almostany configuration is possible through the use of layout manager nestingand area management. In addition, it takes care of the nasty parts ofGUI development (resizing, natural layout, scalability) and leaves you,the programmer, with more time to concentrate on the more interestingparts of your design. Read on to find out more about layout managersand how you can introduce them into your GUIs.

Layout Manager Differences between Java and C#

There are two main differences between layoutmanagers in Java and my implementation of layout managers in C#. Thefirst is that in Java, layout managers are relegated purely to the areaof Graphical User Interfaces. In other words, the only things which canbe laid out by a layout manager are Components. In my implementation,anything which implements the ILayoutable interface is able to be laidout. This opens up many interesting possibilities in the areas ofgraphics, map design, and such. The second difference is related to thefirst. Since layout managers in my implementation are not tied directlyto GUI objects but rather to the abstract concept of laying outlayoutable things, it is possible to have layout managers for distinctareas of a parent Control. You are able to denote a particularrectangle in a parent control and give it a layout manager and add subcontrols to it. This, in addition with the standard layout managermanipulations you know from Java, gives you incredible power in GUIdesign.

How to Use the Layout Library

The Layout library, the implementation oflayout managers which I am presenting in this article, will be familiarto former Java programmers, and hopefully easy to understand for thosefrom other backgrounds. It is based around two interfaces, ILayoutableand ILayoutManager, which extends ILayoutable. The remainder of thelibrary consists of implementations of these two interfaces to thespecific problem of GUI design:

This is the base of the Control layout structure and works as a basic wrapper class for Controls.
Inherits from ControlBox, but is able to add other ControlBoxes and lay them out.
Inherits from ContainerBox, but acts as a top levelpane. It has a rectangle in which it lays out the different Controlswhich have been added to it.
Inherits from AreaPane. The only difference is that it always resizes to be the client size of its parent Control.

Also provided in the library are several sample layouts:

Allows 5 differentLayoutable objects, one in each of the north, south, east and west andone in the center. All the size components are compressed in size,north and south are as small as they can be vertically and as large asthey can be horizontally. West and east are as small as they can behorizontally and as large as they can be vertically. The centerLayoutable object fills all of the space that remains.
Takes a number of rows and columns and creates a grid to which the usercan add Controls. Each member in the grid is made the same size andlaid out from left to right down the rows in the order that they areadded.
Tries to add Controls in a horizontal line, once the line is too bigfor the width of the container, it starts a new line. It can be alignedto the left, center, or right of the container.
Using these classes in GUI creation is straightforward. When you createa Control which will contain other controls, you wrap it in aContainerBox, set the box’s layout, and then add all of the childControls to the box. The only containing Control which is not added toa ContainerBox is the top level Control (usually a Form) which iswrapped in either an AreaPane or a ResizeablePane. A ResizeablePanewill take the shape of its parent Control, but an AreaPane will be thearea which is specified.


Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!