The Windows Form Designer lets you design forms for Windows applications. You can add controls to a form, resize them, move them towherever you want them to be, use the Properties window to change their appearance and behaviour to suit your requirements, and add program code to them to respond to events.
Adding controls to a form
You can add controls to a form in two ways:
- Double-click on a control on the Toolbox. An instance of the control will appear on the form in a default location, with a default size. You can then move and resize the control as required. Note that if you have a container object already selected in the form (for example a GroupBox) , this action places the control inside the container. The result of double-clicking the Button control in the Toolbox is shown below.
- Click on a control in the Toolbox, and move the mouse cursor over the form. The cursor changes while the mouse is over the form to reflect the control type selected (a plus sign with a small image of the control) . Clicking anywhere on the form causes the control to appear at that location with a default size. Alternatively, click and drag to specify both the location and the size of the new control. In the image below, you can see that the cursor has changed to reflect the selection of the Timer control in the Toolbox.
When the control first appears on the form, it is selected, and is displayed surrounded by small white boxes. In the illustartion below, the Button1 control is selected. To select a control that is not currently selected, simply click on it.
You can also select several controls at the same time by dragging a rectangle around them with the mouse. Release the mouse button once all of the required objects are at least partially covered by the rectangle. In the illustration below, both the Button1 and Button2 controls are selected.
Notice that, in the group selection shown above, the Button2 control is surrounded by white boxes, whereas the Button1 control is surrounded by black boxes. The white boxes identify the "master" control. If the commands in the Format menu are applied to the group, the master control is used as a baseline for the other controls in the group. For example, if the Make Same Size > Height command is used, all of the controls in the group are set to be the same height as the master control. You can change the master control by simply clicking on the control that you want to use as the master. Controls can be added to or removed from the group by holding down the Ctrl or Shift key and clicking on the control you want to add or remove. To unselect all controls, press the escape key.
When using a number of controls of the same type and with similar properties on a form, you can do this quickly by creating the first control, editing the properties that will be common to all of the controls, and creating the required number of copies. To make a copy of a control, select it on the form, press Ctrl+C to copy it, then press Ctrl+V to paste a copy back to the form as many times as required. Once you have pasted a copy of a control onto the form, you can drag it into the correct position. You can of course use the Copy and Paste commands from the Edit menu to achieve the same result. Note that, when pasting a copy of a control to your form, the copy is placed inside the currently selected container. You can also copy and paste a group of controls in a similar manner.
Moving and sizing form controls
To move a control on the form, simply click on it, hold the mouse button down, and and drag it into the required position. You can also move a group of controls at the same time. Select the controls to be moved, click on one of the controls in the selection, and hoding down the mouse button drag the control grouping into the required position. Note that you can also drag controls (or groups of controls) out of one container and into another.
To change the size of a control, click on the control to select it, then click and drag one of the white boxes surrounding the control until it has the required dimensions. Note that you can use the boxes at the corners of the control to change both the horizontal and vertical dimensions of the control at the same time. You can resize several controls at the same time by selecting them as a group, and then clicking and dragging one of the boxes surrounding any one of the controls. Once you have the controls arranged on the form in more or less the right position and with suitable dimensions, you can use the commands in the Format menu to fine tune your form. You can, for example, align a group of controls both vertically and horizontally, adjust the spacing between them, and set them all to the same vertical or horizontal dimensions.
When you select a control on a form, the Properties window automatically changes to show the properties for the selected control. You can use the Properties window to view and edit the control's properties. In most cases, you can click on a property and simply type in a new value. other properties are more sophisticated, and provide drop-down lists of options or dialogue boxes, most of which are fairly self explanatory. The illustration below shows the Properties window for a Button control.
Certain properties can be set for a selected group of controls at the same time. If, for example, you have selected a group of TextBoxes, you can use the Properties window to set values for Text, Font, and other properties simultaneously. In the illustration below, three TextBoxes are selected. Note that the Properties window is still available, but the name of the control is ommitted.
Note also the square boxes with the right-pointing arrowhead inside them, known as Smart Tags. Smart Tags are available on many controls, and when clicked will open a small dialogue box that allows one or more properties to be set for the control quickly and easily. The illustration below shows the Smart Tag dialogue box for a TextBox, which enables the Multiline feature to be turned on or off. When the dialogue box is open, the Smart Tag contains a left-pointing arrowhead. Clicking on this closes the dialogue box.
Adding Code to Controls
Once you have added controls to a form and set their properties, you can add code to the form that responds to control events, such as a mouse click, using the Code Editor. You can open the Code Editor by double-clicking a control, which also creates an empty event handler to handle the control's default event. Here is the code that was automatically generated for the control "Button1":