How to Code and Test a Windows Forms Application (2 of 4)

Got this from this webpage

How to Code and Test a Windows Forms Application - How to refer to properties, methods, and events

 
(Page 2 of 4 )

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 its 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 some cases, you will refer to the properties and methods of a class instead of an object that's instantiated from the class. You'll see examples of that in later chapters. For now, you just need to realize that you refer to these properties and methods using the same general syntax that you use to refer to the properties and methods of an object. You enter the class name, a dot, and the property or method name.

To make it easier for you to refer to the members of an object or class, VisualStudio'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 one or more 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 or Enter 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 the ReadOnly property of a text box 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 Select 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 Me keyword is used instead of the name of the form. Here, Me refers to the current instance of the active form. Note also that the names of the methods are followed by parentheses. If a method requires parentheses like these, they're added automatically when you press the Enter key after entering the method name.

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.

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

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 the value 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.Select()

Uses the Select method to move the focus to the text box named txtMonthlyInvestment.

Me.Close()

Uses the Close method to close the form that contains the statement. In this example, Me is a keyword that is used to refer to the current instance of the form 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

  1. 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, type one or more letters of the member name, and Visual Studio will filter the list so that only the members that start with those letters are displayed. You can also scroll through the list to select the member you want. 
  2. Once you've selected a member, you can press the Tab key to insert it into your code, or you can press the Enter key to insert the member and start a new line of code. 
     
  3. By default, all the available members are displayed in the list. To display just the common members, click the Common tab at the bottom of the list. 
     
  4. 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 Basic group, and check the Auto List Members and Parameter Information boxes. 

Figure 3-2.  How to refer to properties, methods, and events

No comments: