The code on the right side of the assignment operator is not just e.X and e.Y. It also calls the ToString method. Before I explain that method, let's examine what happens if you typed the wrong code, leaving out the ToString method, so your code read as follows:
private void Form1_MouseMove
(object sender, MouseEventArgs e) {
lblX.Text = e.X; lblY.Text = e.Y; }
Visual C# 2005 tries to warn you even before you attempt to compile your code. As Figure 3-11 shows, e.X and e.Y both will be underlined with a squiggly line similar to how Microsoft Word highlights misspellings.
Figure 3-11: Incorrect code highlighted.
If you hold your mouse over the underline code, a ToolTip shows with the following warning: "Cannot implicitly convert type 'int' to 'string.'" This warning appears because e.X and e.Y are both integers, whereas the Text properties of the two Label
controls are strings. Visual C# does not permit you to assign an integer to a string.
Undeterred by this warning, you nevertheless attempt to build the project. As Figure 3-12 shows, an Error List should display, reporting the following, similarly to the ToolTip: "Cannot
implicitly convert type 'int' to 'string.'" Additionally, the lines containing this error are identified.
Figure 3-12: Error List reporting an error.
Note If the Error List does not automatically display, you can display it with the menu command View | Other
Windows | Error List.
ToString Method
Of course, you still need to correct the code. To do so, you need to convert the integer value on the right side of the
assignment operator to its string representation. In other words, if the integer is 123, its string representation is "123".
All classes have a ToString method. What that method does depends on the class. In the case of the Int32 class, which represents an integer, the ToString method converts an integer to the string representation of the integer, so it can be assigned to the Text property of the Label controls.
The ToString method is preceded by the integer value to be converted and a dot or period. It is followed by empty
parentheses because this method has no parameters.
Note Though the parentheses are empty, do not omit them because a compiler error will result.
Delegate
Figure 3-12 shows Solution Explorer with the expander next to Form1.cs to show two files under it, one of which is
Form1.Designer.cs. (You may need to click the Show All Files button to obtain this view.) Right-click that file name and choose View Code from the shortcut menu. This will display the code in Form 1.Designer. cs, as shown in Figure 3-13.
Figure 3-13: Code view of Form1.Designer.cs.
One of the lines of code reads (here on three lines because of its length):
this.MouseMove +=
new System.Windows.Forms.MouseEventHandler (this.Form1_MouseMove);
As explained in Chapter 2, when the .NET Framework that underlies Visual C# 2005 detects an event, such as the mouse button being held down, that happens to an object such as a form, its searches for an event procedure that handles that event for that object. If the .NET Framework finds such an event procedure, it calls that event procedure, and the code inside the event procedure executes.
MouseEventHandler, part of the System.Windows.Forms namespace, is a delegate. A delegate is used to specify which procedure handles an event that happens to a particular object. MouseEventHandler in particular specifies the procedure that will handle the MouseDown, MouseUp, or MouseMove event of a form, control, or other component.
The += operator is explained in Chapter 5 on arithmetic operators. For now, treat it as an assignment operator.
On the left side of the += operator is this.MouseMove. The "this" keyword refers to the current object of the Form1 class—that is, the form over which the mouse button is being held down. MouseDown is the event. Accordingly, this.MouseMove specifies the event to be handled, which is the mouse moving over the form.
On the right side of the += operator, the MouseEventHandler delegate is followed in parentheses by the name of the procedure that will handle the event, Form1_MouseMove.
Note If you delete an event procedure, you will get a compiler error if you don't delete the line concerning the corresponding delegate in Form1.Designer.cs.
Conclusion
The form is perhaps the most important control. However, a single form without controls could only satisfy the requirements of the simplest Windows application. The form does not permit the typing of text, listing data, selecting of choices, and many other tasks that an application may need to perform. You need other, specialized controls for that additional functionality. Indeed, the form's primary role is to serve as a host, or container, for controls such as menus, toolbars, and buttons, which enrich the GUI of Windows applications.
This chapter showed you how to add controls to your form using the Toolbox. You then learned how to use the Forms Designer to change the size and location of the controls. The project also showed you how to control the size and location of multiple controls relative to each other.
The Label class, like the Form class, has properties. Perhaps the most important properties of the Label class are its Name and Text properties.
The Name property determines how you refer to a label in code. You should use a naming convention when naming a label that you will refer to in code. This chapter suggested a naming convention using a prefix, usually all lowercase and consisting of three letters, that indicates the type of control it is, followed by a word, first letter capitalized, that suggests its purpose.
The Text property determines the value of the text displayed by the label. Like the Text property of the Form class, you can change the value of the Label control's Text property either at design time or through code. You generally will use the
Properties window if the purpose of the label is to identify the purpose of another control because that information usually will not change during the running of the application. By contrast, you generally will use code if the purpose of the label is to display data that may change during the running of the application. This code often will be located inside of an event procedure.
This chapter included a project that uses the Label control for both purposes—to display data that does not change during the
running of the application and to display data that does change during the running of the application. Finally, you learned how to use information called parameters that's available to an event procedure.
Although it is impressive that you can create a working Visual C# 2005 program that displays information using controls by writing only two lines of code, most programs need to save information, or data. The next chapter will teach you about the different data types as well as how to create and use
Quiz
1. What are examples of controls?
2. What is the purpose of the Toolbox?
3. How do you add a control from the Toolbox onto your form?
4. What is the purpose of the Name property of a control?
5. What is a naming convention?
6. What characteristic of the Label control does its Text property determine?
7. What are purposes of the text displayed by a Label control?
8. Can a single statement in C# take up two or more lines in the code editor?
9. What is a parameter of an event procedure?
10. What is a delegate?
Answers
1. TextBox, Label, ListBox, and Button are all controls.
2. The purpose of the Toolbox is to display controls that you can add to your form.
3. You may add a control from the Toolbox onto your form either by doubleclicking the control in the Toolbox or by dragging the control from the Toolbox and then dropping it onto the form,
4. The Name property of a control is used to identify that control in code.
5. A naming convention is a consistent method of naming, such as when naming controls. 6. The value of the Text property of a Label control determines the text that will be displayed by
the label.
8. A single statement in C# may take up two or more lines in the code editor. 9. A parameter represents information that is available to an event procedure.
10. A delegate is used to specify which procedure handles an event that happens to a particular object.