The first step in designing your application’s interface is, of course, the analysis and careful planning of the basic operations you want to provide through your interface. The second step is to design the forms. Designing a form means placing Windows controls on it, setting the controls’ properties, and then writing code to handle the events of interest. Visual Studio 2008 is a rapid application development (RAD) environment. This doesn’t mean that you’re expected to develop applications rapidly. It has come to mean that you can rapidly prototype an application and show something to the customer. And this is made possible through the visual tools that come with VS 2008, especially the new Form Designer.
To place controls on your form, you select them in the Toolbox and then draw, on the form, the rectangle in which the control will be enclosed. Or you can double-click the control’s icon to place an instance of the control on the form. All controls have a default size, and you can resize the control on the form by using the mouse.
Each control’s dimensions can also be set in the Properties window through the Size property. The Size property exposes the Width and Height components, which are expressed in pixels. Likewise, the Location property returns (or sets) the coordinates of the top-left corner of the control. In the section “Building Dynamic Forms at Runtime,” later in this chapter, you’ll see how to create new controls at runtime and place them in a specific location on a form from within your code.
As you place controls on the form, you can align them in groups by using the commands of the Format menu. Select multiple controls on the form by using the mouse and the Shift (or Ctrl) key, and then align their edges or their middles with the appropriate command of the Format menu. To align the left edges of a column of TextBoxes, choose the Format Align Left command. You can also use the commands of the Format Make Same Size command to adjust the dimensions of the selected controls. (To make them equal in size, make their widths or heights equal.)
As you move controls around with the mouse, a blue snap line appears when the controls become nearly aligned with another control. Release the mouse while the snap line is visible to leave the control aligned with the one indicated by the snap lines. The blue snap lines indicate edge alignment. Most of the time, we need to align not the edges of two controls, but their baselines (the baseline of the text on the control). The snap lines that indicate baseline alignment are red. Figure 5.2 shows both types of snap lines. When we’re aligning a Label control with its matching TextBox control on a form, we want to align their baselines, not their frames (especially if you consider that the Label controls are always displayed without borders). If the control is aligned with other controls in both directions, two snap lines will appear— a horizontal one and a vertical one.
Figure 5.2 – Edge alignment (top) and baseline alignment (bottom) when placing control on a form