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.
Setting Control Properties
After you've added controls to a form, you can use the Properties window to view and change their property values. If you have more than one control selected, the Properties window shows only the properties that the controls have in common.
For example, if you select a TextBox and a Label, the Properties window shows the Text property because both Labels and TextBoxes have a Text property. However, it won't display the Multiline property because the TextBox control has that property but the Label control does not.
The Properties window provides special support for many control properties. For example, Figure 2.2 shows the Properties window when a TextBox is selected.
Notice that the Font property contains its own sub-properties: Name, Size, Unit, Bold, and so forth. Click the plus or minus sign next to a property to expand or collapse it and show or hide its sub-properties.
Also notice in Figure 2.2 the ellipsis to the right of the Font property. If you click that ellipsis, the dialog shown in Figure 2.3 appears. You can use this dialog to edit the font sub-properties and see a sample of the font.
The Properties window provides appropriate support when it can for other properties. Many properties can hold only certain values. For example, the Font's Italic, Bold, Strikeout, and Underline sub-properties can only take the values True or False. The Font's Unit sub-property can only take the values World, Pixel, Point, Inch, Document, and Millimeter. In these cases, the Properties window provides a dropdown listing the allowed choices.
Figure 2.4 shows the editor that the Properties window displays when you click the dropdown arrow to the right of a TextBox's BackColor property. The Custom tab lets you pick a color from a palette, the Web tab lets you pick standard web page colors, and the System tab lets you pick system colors such as the normal control background color or the menu highlight color.
By using the Properties window's editors and typing in values when there is no editor, you can change a control's appearance and behavior.
Whenever you create a control, Visual Studio gives it a rather nondescript name such as label2, textBox5, or pictureBox1. Although these names tell you what kind of object the control is, they don't tell you what it is for and that's much more important when you later need to use the control in your code. Names like firstNameTextBox, hatSizeTrackBar, and mediaTypeComboBox are much more meaningful than textBox3 and textBox7.
Note that you don't need to give good names to every control, just the ones that you will need to use later in the code. You often don't need to name Labels, GroupBoxes, and other purely decorative controls.
You can learn more about Microsoft's naming conventions on the web page “Guidelines for Names” at msdn.microsoft.com/library/ms229002.aspx.
What's in a Name, Redux
Earlier in this lesson I said that control type names use Pascal casing. By convention, the names of specific instances of controls use camel casing, where multiple words are strung together with the first letter of each word capitalized, except for the first word. For example, the control type TextBox uses Pascal casing and the specific control name firstNameTextBox uses camel casing.
It's called camel casing because it sort of looks like a camel lying down: low at the ends with one or more humps in the middle. I guess stateLabel would be a dromedary (one-humped) camel, priceTextBox would be a Bactrian (two-humped) camel, and numberOfEmployeesCoveredByInsurancePlanTrackBar would be some sort of camel created by Dr. Seuss.
What's in a Name, Part 3
Most C# developers add a control's type as a suffix to its name as in firstNameTextBox or resultLabel, but it's becoming more common for developers to use a more generic word such as value or field. The idea is that if you decide to change the type of control that handles the value, you won't need to change the code that refers to the control.
For example, suppose your program uses a TrackBar to let the user select the number of UFO detectors to purchase. If you name this control numUfoDetectorsValue, then you won't need to change the code if you later decide to let the user select the value from a NumericUpDown control instead of a TrackBar.
Some developers even omit the suffix completely as in numUfoDetectors, although that can be confusing if you need more than one control to represent a similar concept or if you want a variable inside the code that holds the numeric value represented by the control.
For now, I recommend that you stick with the control's full type name as a suffix.
You'll learn about key control properties as you go along, but for now Table 2.1 summarizes some of the most useful properties. Note that not all controls have every property. For example, a Button cannot display a border (or it always displays a border, depending on your point of view) so it has no BorderStyle property.
If you want some practice with these properties, create a new project and give them a try. Create a Button and set its Text property. Also click the form and set its Text property. Change the form's Font property and see what happens to the form and the button it contains. Experiment with some of the other properties such as Image and ForeColor if you like.
This lesson doesn't really go into handling control events very much (that's the subject of Lesson 4), but I do want to explain how to set properties in code and you need event handlers to do that. Besides, it's easy and sort of fun, and it'll let you make a program that does something more than just sitting there looking pretty.
To make a simple event handler for a control, double-click the control in the Form Designer. That opens the Code Editor and creates an empty event handler for the control's default event.