no edit summary
[[Image:visual_studio_toolbox.jpg| Visual Studio Toolbox]]
Visual Studio provides three methods for
added new controls to a form. In this chapter we will cover each of these different approaches.
== Double Clicking the Control in the Toolbox ==
The Toolbox contains all of the controls available to be added to a form. One way to add a control to form is to simply double click on the control in the Toolbox. The control is then automatically added to the current form. The advantage of this approach is that it is very quick to add multiple controls to a form. The downside is that Visual Studio does not know where you want the control positioned in the form and consequently positions the controls near to the top left hand corner of the form. Once the control is added to the form you will need to click drag it to the desired location.
== Dragging a Dropping Controls onto the Form ==
Another approach to adding controls to a form is to click on the control in the Toolbox and drag it onto the form. When the control is in the required location release the mouse button to drop the control into place. The position of the control can subsequently be refined using the mouse or arrow keys to move the location of the control.
== Drawing a Control on the Form ==
The most precise mechanism for adding a control to a form is to draw it. This is achieved by first selecting the desired control in the Toolbox. Once the control is highlighted, move the mouse pointer to the location on the form where you would like the top left hand corner of the new control to appear. Click and hold down the left hand mouse button
a drag the pointer. A box will be drawn as you drag the pointer. When the box reaches the required size for the new control, release the mouse button. The new control will appear position and sized according to the size and location of the box.
== Positioning and Sizing Controls Using the Grid ==
When adding controls to a form it is possible to configure how controls are positioned and sized by activating a grid. When the grid is active, controls "snap" to the nearest grid position when added to a form.
There are a number of ways to configure the granularity and behavior of the Visual Studio grid. These settings are global in that
, once defined, they apply to all forms and projects, not just to the current form or project. Note , also that for some reason, changes to the grid to not immediately take effect in forms in which you are already working.
Grid settings are changed using the Visual Studio ''Options'' screen. To access this screen, select ''Options...'' from the ''Tools'' menu. When the dialog appears, click on ''Windows Forms Designer'' in the left hand tree. Once selected the dialog should appear as follows:
* '''GridSize''' - This setting controls the vertical and horizontal distances between the grid points on the form. This essentially controls the granularity of the grid. The smaller the gaps between grid points, the greater control over control size and position.
* '''ShowGrid''' - Controls whether the grid dots are visible in the form. Note that this setting
control only whether the grid is visible, not whether controls snap to the grid locations. Snapping is controlled by the ''SnapToGrid'' setting.
* '''SnapToGrid''' - This setting determines whether the grid is used when controls are added. When set to ''True'' new controls will "snap" to the nearest grid location when added. When set to ''False'' the grid is ignored and controls will appear where they are dropped or drawn.
== Positioning Controls Using Snap Lines ==
One of the key objectives in designing esthetically pleasing forms is getting controls aligned. One way to make the task of aligning controls involves the use of "Snap Lines" in Visual Studio. When activated, the Snap Lines feature causes a line to be drawn between an edge of the control you are currently moving and the corresponding edge of the closest control on the form when the edges are in alignment.
To active Snap Lines select ''Options...'' from the ''Tools'' menu, click on ''Windows Form Designer'' in the tree to the left of the Options dialog and configure the following settings:
* ShowGrid: False
Once the settings are
apply add a Button control to a form. Next, click on the ''ListBox'' control in the ToolBox and drag it over to the form. As you move the ListBox below the Button a line will appear between the controls at any point that edges align. For example the following figures show a line appearing at the point the left and right hand edges of the controls align:
[[Image:visual_studio_layout_toolbar.jpg| The Visual Studio Layout Toolbar]]
Select a group of components and click on the various buttons in the toolbar to see the effect. For example, all the controls can be left or right aligned and resized to the same size, width or height. It is also possible to equally space the controls, and then increase or decrease the spacing used to separate the controls. It is also possible
pace controls on top of one another and change which control appears on top of the stack (this can also be done in Visual Basic code using the BringToFront() and ''SendToBack()'' methods of the respective controls).
All together, the selection of controls
allow just about any uniform layout to be achieved from a group of controls in a form.
== Setting Properties on a Group of Controls ==
[[Image:visual_studio_tab_oder.jpg| Setting Tab Order in Visual Studio]]
To change the order, simply click on each control in the order you wish for them to be navigated by the
tab key. As you click on each control the number will change, starting at 0 until all the controls have been sequenced. The tab order is now set.