Читать книгу C# 24-Hour Trainer - Stephens Rod - Страница 23
Section I
The Visual Studio IDE and Controls
Lesson 2
Creating Controls
Creating Controls
ОглавлениеAdding controls to a form is easy. In fact, it's so easy and there are so many different ways to add controls to a form that it takes a while to describe them all.
Start by creating a new project as described in Lesson 1. Open the form in the Form Designer. (If the form isn't already open, double-click it in Solution Explorer.)
The following list describes some of the ways you can put controls on the form:
● Click a tool in the Toolbox to select it. Then click and drag on the form. When you release the mouse, Visual Studio creates the control in the area you selected and then selects the pointer in the Toolbox.
● Click a tool in the Toolbox to select it. Then hold down the Ctrl key while you click and drag on the form to place a copy of the control on the form. When you release the mouse, Visual Studio creates the control in the area you selected and keeps the control's tool selected in the Toolbox so you can make another control of that type.
● Double-click a tool in the Toolbox to create an instance of the control on the form at a default size and position. (You'll then probably want to resize and reposition it.)
● Select one or more controls that are already on the form, press Ctrl+C to copy them, and then press Ctrl+V to paste them onto the form. You can even copy and paste from one instance of Visual Studio to another.
● Select one or more controls on the form. While holding down the Ctrl key, drag the controls to a new location. Visual Studio makes a copy of the controls, leaving the originals where they started.
NOTE
You have several ways to select controls on the Form Designer. Click a control to select only it. Click and drag to select multiple controls.
Hold down the Shift or Ctrl key while clicking or clicking and dragging to toggle whether controls are in the current selection.
And, if you want to deselect all controls, simply click an empty part of the form or press Esc.
The first method (select a tool and then click and drag to create a control) is probably used most often, but some of the other methods are particularly useful for creating groups of similar controls.
For example, the form in Figure 2.1 displays five rows, each of which holds a Label and a TextBox. You could easily build all of these controls individually, but you can build them even faster by using copy and paste. First place one Label and TextBox on the form, arrange them next to each other, and give them any property values that you want all of the Labels or TextBoxes to share. (For example, you may want to set their fonts or colors.) Now click and drag to select both controls, copy and paste, and drag the new controls into position. Repeat this three more times and you'll have all of the controls in position. You'll still need to change the Labels' text but the basic arrangement will be done without going back and forth to the Toolbox.