FREE BOOK

Chapter 3: How to code and test a Windows Forms application using C# 2008

Posted by Murach Free Book | Windows Forms December 10, 2008
In this chapter, you'll learn how to code and test a Windows Forms application. When you're done, you'll be able to develop simple applications of your own.

How to refer to properties, methods, and events

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 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 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

this.Close();

Uses the Close method to close the form that contains the statement. In this example, this is 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 one or more 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 or Enter 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.

How an application responds to events

Windows Forms applications are event-driven. That means they work by responding to the events that occur on objects. To respond to an event, you code a special type of method known as an event handler. When you do that, Visual Studio generates a statement that connects, or wires, the event handler to the event. This is called event wiring, and it's illustrated in figure 3-3.

In this figure, the user clicks the Exit button on the Invoice Total form. Then, Visual Studio uses the statement it generated to wire the event to determine what event handler to execute in response to the event. In this case, the btnExit.Click event is wired to the method named btnExit_Click, so this method is executed. As you can see, this event handler contains a single statement that uses the Close method to close the form.

This figure also lists some common events for controls and forms. One control event you'll respond to frequently is the Click event. This event occurs when the user clicks an object with the mouse. Similarly, the DoubleClick event occurs when the user double-clicks an object.

Although the Click and DoubleClick events are started by user actions, that's not always the case. For instance, the Enter and Leave events typically occur when the user moves the focus to or from a control, but they can also occur when code moves the focus to or from a control. Similarly, the Load event of a form occurs when a form is loaded into memory. For the first form of an application, this typically happens when the user starts the application. And the Closed event occurs when a form is closed. For the Invoice Total form presented in this figure, this happens when the user selects the Exit button or the Close button in the upper right corner of the form.

In addition to the events shown here, most objects have many more events that the application can respond to. For example, events occur when the user positions the mouse over an object or when the user presses or releases a key. However, you don't typically respond to those events.

Event: The user clicks the Exit button



Figure 3-3 How an application responds to events

Wiring: The application determines what method to execute

  
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);

Response: The method for the Click event of the Exit button is executed

private void btnExit_Click(object sender, System.EventArgs e)
{
    this.Close();
}

Common control events




Common form events



Concepts

  • Windows Forms applications work by responding to events that occur on objects.

  • To indicate how an application should respond to an event, you code an event handler, which is a special type of method that handles the event.

  • To connect the event handler to the event, Visual Studio automatically generates a statement that wires the event to the event handler. This is known as event wiring.

  • An event can be an action that's initiated by the user like the Click event, or it can be an action initiated by program code like the Closed event.

Total Pages : 10 12345

comments