Published on System iNetwork (http://systeminetwork.com)
Line Up Windows Controls with TableLayoutPanel
By dcronk
Created Feb 19 2008 - 16:26

By:
Craig Pelkie [1]

One of the issues that you run into when you design a Windows form with many labels and textboxes is that it can take quite a while to get everything lined up nicely.

Even though Visual Studio now includes guidelines that help you place controls onto forms at design time, it still takes a steady hand on the mouse to get things exactly in place.

The alternative of selecting controls and explicitly setting X/Y positions is not very enticing.

To help you get things together, consider using the TableLayoutPanel, provided in the Visual Studio Toolbox (Containers) section, as shown in Figure 1 [2]. When you place this control on your form, it initially appears as shown in Figure 1 [3] as just a simple grid.

To get the most out of the tool, you can click its smart-tag (upper right corner of the tool) and set rows and columns, as shown in Figure 2 [4].

You can add as many rows and columns as needed and set the size you need. It helps if you know in advance how many rows and columns you want; I usually make a sketch of the form before starting, count my rows and columns, then start using the tool.

You're not locked into the exact numbers of rows and columns you specify, nor are you locked into the sizing. You can easily add and remove, and adjust sizes, either in this dialog or on the control itself.

The real power of this tool comes into play when you start dropping other controls onto it. Figure 3 [5] shows an example of three labels and three textboxes. By default, the controls are left-aligned within the cell that I dropped them into.

Once in the cell, you can fine-tune the horizontal and vertical alignment of each control. If you need a longer textbox, you can indicate that adjacent cells of the table are to be merged.

You can get a good idea of how to use this tool by simply going through the steps I've described here and then running the application. You don't need any more controls or code than what I've shown to see the effect. At runtime, the table is not displayed, just the controls that you loaded onto the form.

For relatively simple forms with only a few controls, the TableLayoutPanel is probably more bother than it's worth. However, once you start working with a larger set of controls on a form, you'll find it really helps to keep everything lined up.

Craig Pelkie has worked as a programmer with IBM midrange computers for many years. He has also written and lectured extensively on AS/400 and System i technologies, including client/server programming, Client Access, Java WebSphere, .NET applications for the System i, and web development. You can reach him at cpelkie@systeminetwork.com [6].

Copyright © Penton Media

Source URL: http://systeminetwork.com/article/line-windows-controls-tablelayoutpanel

Links:
[1] http://systeminetwork.com/author/craig-pelkie
[2] http://pentontech.com/IBMContent/Images/article/56329_50535_table_fig01.jpg
[3] http://pentontech.com/IBMContent/Images/article/56329_50535_table_fig01.jpg
[4] http://pentontech.com/IBMContent/Images/article/56329_50536_table_fig02.jpg
[5] http://pentontech.com/IBMContent/Images/article/56329_50537_table_fig03.jpg
[6] mailto:cpelkie@systeminetwork.com