Stephens Rod

C# 24-Hour Trainer


Скачать книгу

the Click event. Whenever the user clicks the control at run time, it raises its Click event and this code executes.

      To change a property in code, type the control's name, a dot (or period), the name of the property, an equals sign, and finally the value that you want to give the property. Finish the line of code with a semicolon. For example, the following statement sets the Left property of the label named greetingLabel to 100. That moves the label so it's 100 pixels from the left edge of its container:

      The following code shows a complete event handler:

      In this code, I typed the first line that starts with two slashes. That line is a comment, a piece of text that is contained in the code but that is not executed by the program. Any text that comes after the // characters is ignored until the end of the current line. You can (and should) use comments to make your code easier to understand. They don't make the executable program bigger or slower, so don't be stingy with your comments!

      I also typed the line that sets the Label's Left property.

      Visual Studio typed the rest when I double-clicked the moveLabelButton control. You don't need to worry about the details of this code right now, but briefly the sender parameter is the object that raised the event (the Button in this example) and the e parameter gives extra information about the event. The extra information can be useful for some events (for example, in the MouseClick event it tells where the mouse was clicked), but it's not very interesting for a Button's Click event.

      Simple numeric values such as the 100 used in this example are easy to set in code, but some properties aren't numbers. In that case, you must set them to values that have the proper data type.

      For example, a Label's Text property is a string so you must give it a string value. The following code sets the greetingLabel control's Text property to the string Hello:

      NOTE

      Notice that you must include the string Hello in double quotes to tell C# that this is a literal string and not some sort of C# command. If you leave the quotes off, C# gets confused and gives you the error “The name ‘Hello’ does not exist in the current context.”

      Over time, you'll get used to messages like this and they'll make sense. In this case, the message just means, “I don't know what the word ‘Hello’ means.”

      Other property values have more exotic data types such as Date, AnchorStyles, Point, and BindingContext. When you set these properties, you must make sure that the values you give them have the correct data types. I'm going to ignore most of these for now, but one data type that is relatively simple and useful is Color.

      A control's ForeColor and BackColor properties have the data type Color so you cannot simply set them equal to strings such as Red or Blue. Instead you must set them equal to something that also has the type Color. The easiest way to do that is to use the colors predefined by the Color class. This may seem a bit confusing, but in practice it's actually quite easy.

      For example, the following two statements set a Label's BackColor and ForeColor properties to HotPink and Blue, respectively:

      The following code shows how the MoveButton example program, which is available as part of this lesson's code download on the book's website, changes several Label properties when you click a Button:

      Arranging Controls

      The Form Designer provides several tools to help you arrange controls at design time. The following sections describe some of the most useful: snap lines, arrow keys, the Format menu, and the Layout toolbar.

Snap Lines

When you drag a control around on the form, the Form Designer displays snap lines that show how the control lines up with the form and with other controls. Figure 2.5 shows the Form Designer displaying light blue snap lines indicating that the control is standard distances away from the form's top and left edges.

Figure 2.5

      You can drag the control away from this position and, if you do so, the snap lines disappear. When you drag the control close to one of the form's edges, the control jumps to the standard distance and the Form Designer displays the snap lines again.

The Form Designer also displays snap lines to show how controls align. In Figure 2.6, I dragged a second Button below the first. Different snap lines show that:

      ● The second Button is the standard distance from the form's left edge.

      ● The second Button's left and right edges line up with the first Button's edges.

      ● The second Button is a standard distance below the first Button.

Figure 2.6

Other snap lines show how the control contents line up. In Figure 2.7 snap lines show that the Label is the standard distance from the second Button and that the Label's text baseline lines up with the baseline of the second Button.

Figure 2.7

For a more realistic example, consider Figure 2.8. In this figure I was laying out a small data entry form, and I wanted all of the Labels and TextBoxes to line up nicely. In this figure, snap lines show that the Street TextBox is lined up on the left and right with the other TextBoxes, is a standard distance from the TextBoxes above and below, is a standard distance from the form's right edge, and has its baseline lined up with the Street Label.

Image described by surrounding text.

Figure 2.8

Arrow Keys

      In addition to dragging controls with the mouse, you can move controls by pressing the arrow keys. Select one or more controls and then use the left, right, up, and down arrow keys to move the control(s) one pixel at a time. This method is slower than using the mouse but gives you finer control.

      When you move controls with the arrow keys, the Form Designer doesn't display snap lines so you may want to keep an eye on the control's Location property in the Properties window to see where it is.

The Format Menu and Layout Toolbar

The Format menu contains many commands that arrange one or more controls. Table 2.2 summarizes the Format menu's submenus.

Table 2.2

      The Layout toolbar contains the same commands as the Format menu but in a handy toolbar so they're easier to use. The buttons display little pictures that show how they align controls.

      NOTE

      How these tools arrange controls depends on how you select the controls. One of the selected controls, normally the first one you select, is the group's dominant control. The dominant control is marked with white boxes at its corners, whereas the other controls are marked with black boxes.

      When you use an arranging tool, the dominant control determines how the others are arranged. For example, if you select the Format 1 Align 1