How to Code and Test a Windows Forms Application - How to refer to properties, methods, and events
(Page 2 of 5 )
As you enter the code for a form in the Code Editor window, you often need to refer to the properties, methods, and events of the form’s objects. To do that, you type the name of the object, a period (also known as a
dot operator, or
dot), and the name of the member. This is summarized in figure 3-2.
In addition to referring to the properties, methods, and events of objects, you can also refer to some of the properties and methods of a class directly from that class. The code shown in the Code Editor in this figure, for example, refers to the ToDecimal method of the Convert class. A property or method that you can refer to directly from a class like this is called a static member. You’ll learn more about static members in chapter 4. For now, you just need to realize that you can refer to static properties and methods using the same techniques that you use to refer to the properties and methods of an object.
To make it easier for you to refer to the members of an object or class, Visual Studio’s IntelliSense feature displays a list of the members that are available for that class or object after you type a class or object name and a period. Then, you can highlight the entry you want by clicking on it, typing the first few letters of its name, or using the arrow keys to scroll through the list. In most cases, you can then complete the entry by pressing the Tab key.
To give you an idea of how properties, methods, and events are used in code, this figure shows examples of each. In the first example for properties, code is used to set the value that’s displayed for a text box to 10. In the second example, code is used to set a text box’s ReadOnly property to true. Although you can also use the Properties window to set these values, that just sets the properties at the start of the application. By using code, you can change the properties as an application is running.
In the first example for methods, the Focus method of a text box is used to move the focus to that text box. In the second example, the Close method of a form is used to close the active form. In this example, the this keyword is used instead of the name of the form. Here, this refers to the current instance of the active form. Note also that the names of the methods are followed by parentheses.
As you progress through this book, you’ll learn how to use the methods for many types of objects, and you’ll learn how to supply arguments within the parentheses of a method. For now, though, just try to understand that you can call a method from a class or an object and that you must code a set of parentheses after the method name.
Although you’ll frequently refer to properties and methods as you code an application, you’ll rarely need to refer to an event. That’s because Visual Studio automatically generates the code for working with events, as you’ll see later in this chapter. To help you understand the code that Visual Studio generates, however, the last example in this figure shows how you refer to an event. In this case, the code refers to the Click event of a button named btnExit.
A member list that’s displayed in the Code Editor window

Figure 3-2. How to refer to properties, methods, and events
The syntax for referring to a member of a class or object
ClassName.MemberName
objectName.MemberName
Statements that refer to properties
| txtTotal.Text = "10"; | Assigns a string holding the number 10 to the Text property of the text box named txtTotal. |
| txtTotal.ReadOnly = true; | Assigns the true value to the ReadOnly property of the text box named txtTotal so the user can’t change its contents. |
Statements that refer to methods
txtMonthlyInvestment.Focus(); | Uses the Focus method to move the focus to the text box named txtMonthlyInvestment. |
this.Close(); | Uses the Close method to close the form that contains the statement. In this example, thisis a keyword that is used to refer to the current instance of the class. |
Code that refers to an event
btnExit.Click | Refers to the Click event of a button named btnExit. |
How to enter member names when working in the Code Editor
- To display a list of the available members for a class or an object, type the class or object name followed by a period (called a dot operator, or just dot). Then, you can type the first few letters of the member name, and the Code Editor will select the first entry in the list that matches those letters. Or, you can scroll down the list to select the member you want. Once it’s selected, press the Tab key to insert the member into your code.
- If a member list isn’t displayed, select the Tools->Options command to display the Options dialog box. Then, expand the Text Editor group, select the C# category, and check the Auto List Members and Parameters Information boxes.
Next: How an application responds to events >>
More C# Articles
More By Murach Publishing
|
This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.
|
|