I would like to bring back to mind the advantages of using a layout manager (LM) for your smart device .NET projects.
None of the layout managers mentioned here are developed by me. I don't have the time to do so, especially when there are great free solutions on the net.
On full .NET Framework, you have the tablelayout panel where you can place your GUI elements and the elements will be arranged automatically, when the main form is resized.
OK, on smart devices, there will be less form resizing as mostly the forms are full screen by design (QVGA:240×320 or VGA:480×640). But what happens to your form design, when the user rotates the screen from portrait to landscape orientation?
public DialogTest2 ()
TextBox textbox = new TextBox();
Button btnExit=new Button();
Button btnSwitch=new Button();
To adjust the positions and sizes of these 3 elements, you would need to change top, left and size property of every element in the forms' resize event handler.
Positioning and Sizing your GUI Elements by Hand
Now, you write code for the Form Resize event and set top, left and size of all your GUI elements by hand. That are three statements for every element and again for the two possible orientations. Having only four elements on your form, that will be 24 lines to code! Stupid coding, I thought and remembered the LMs available for JAVA. So I tried to find JAVA-like layout managers for .NET. First, I did not find any, as the full framework already provides a LM and no one seems to have a need for writing their own LM. But finally, I found three LMs on the internet.
What is the Purpose of a Layout Manager
A LM should arrange GUI elements inside a container (a form or dialog) according to some well known layout pattern. Instead of writing many code lines to position and resize your GUI elements, the LM will do this for you and place and size the elements according to the LM's rules. Of course you have to write additional statements to get an automatic layout, but you need only write once and it will work for portrait and landscape orientation.
All the LMs I found work similar. You have to add a container, possibly specify a layout, and then add your GUI elements to the LM.
The Layout of your GUI Elements
There are different layouts available:
- Flow Layout – positions GUI elements like written text, from left to right and top to bottom
- Table or Grid Layout – provides a table of cells in rows and columns. Each element is added to the next free cell
- Border Layout – the available space is divided into 5 zones, north, west, center, east and south. You can have one element (also containers with several elements) at each of these locations.
The Layout Managers I Found
- Drop-dead easy LM (Link) (no further use here)
- Grid Layout (Link) (no further use here)
- Single Column FlowLayout (Link) (no further use here)
- Layout Managers in C# (Link) (great compact framework compatible LM)
- Simple Layout Managers (Link) (looks also impressive)
FlowLayoutPanel for CF (Link) (This was the first one I found on the net.)
DotNetLayout (SourceForge Link)
public class DialogTest : Form
public DialogTest ()
panel = new ResizeablePane(this, ClientRectangle, new BorderLayout());
Panel panelNorth=new Panel();
ContainerBox boxNorth=new ContainerBox(panelNorth);
label=new Label(); label.Name="label";
label.Size=new Size(100, 26);
textbox=new TextBox(); textbox.Name="textbox";
textbox.Size=new Size(100, 26);
Panel panelSouth=new Panel();
ContainerBox boxSouth=new ContainerBox(panelSouth);
button= new Button(); button.Name="button";
btn_Size=new Button(); btn_Size.Name="btn_size";
Button btnTest=new Button();
btnTest.Click += new EventHandler(btnTest_Click);
Panel panelCenter=new Panel();
ContainerBox boxCenter=new ContainerBox(panelCenter);
panel1=new Panel(); panel1.Name="panel1";
picturebox=new PictureBox(); picturebox.Name="picturebox";
The code above will produce this automatic layout:
In the above code, all GUI elements are created and sized manually. You can also design your Form with the Form designer in Visual Studio and then add the existing elements to the layout panels. But don't use
foreach with the form elements, as the order is reversed or unwanted.
The code sample shows how to use a
BorderLayout, panels to group elements and nesting layouts using a different LM like a grid layout.
<!-- Social Bookmarks BEGIN -->
<!-- Social Bookmarks END -->