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 add code to a form

Now that you understand some of the concepts behind object-oriented coding, you're ready to learn how to add code to a form. Because you'll learn the essentials of the C# language in the chapters that follow, though, I won't focus on the coding details right now. Instead, I'll focus on the concepts and mechanics of adding the code to a form.

How to create an event handler for the default event of a form or control

Although you can create an event handler for any event of any object, you're most likely to create event handlers for the default events of forms and controls. So that's what you'll learn to do in this chapter. Then, in chapter 6, you'll learn how to create event handlers for other events.

To create an event handler for the default event of a form or control, you double-click the object in the Form Designer. Then, Visual Studio opens the Code Editor, generates a method declaration for the default event of the object, and places the insertion point on a blank line between the opening and closing braces of that declaration. As a result, you can immediately start typing the C# statements that you want to include in the body of the method.

To illustrate, figure 3-4 shows the code that was generated when I doubleclicked the Calculate button on the Invoice Total form. In this figure, the code for the form is stored in a file named frmInvoiceTotal.cs. In addition, the name of the method is the name of the object (btnCalculate), an underline, and the name of the event (Click). The statement that wires the Click event of this button to this event handler is stored in the file named frmInvoiceTotal.Designer.cs.

Before you start an event handler for a control, you should set the Name property of the control as described in chapter 2. That way, this name will be reflected in the method name of the event handler as shown in this figure. If you change the control name after starting an event handler for it, Visual Studio will change the name of the object in the event wiring, but it won't change the name of the object in the method name. And that can be confusing when you're first learning C#.

You should also avoid modifying the method declaration that's generated for you when you create an event handler. In chapter 6, you'll learn how to modify the method declaration. But for now, you should leave the method declaration alone and focus on adding code within the body of the method.

How to delete an event handler

If you add an event handler by mistake, you can't just delete it. If you do, you'll get an error when you try to run the application. This error will be displayed in an Error List window as shown in figure 3-7, and it will indicate that the event handler is missing.

The method that handles the Click event of the Calculate button



Figure 3-4 How to create an event handler for the default event of a form or control


How to handle the Click event of a button

  1. In the Form Designer, double-click the control. This opens the Code Editor, generates the declaration for the method that handles the event, and places the cursor within this declaration.

  2. Type the C# code between the opening brace ({) and the closing brace (}) of the method  declaration.

  3. When you are finished writing code, you can return to the Form Designer by clicking the View Designer button in the Solution Explorer window.

How to handle the Load event for a form

  • Follow the procedure shown above, but double-click the form itself.

Description

  • The method declaration for the event handler that's generated when you double-click on an object in the Form Designer includes a method name that consists of the object name, an underscore, and the event name.

  • The event handler is stored in the cs file for the form.

  • Most of the code that's generated when you design a form, including the statement  that wires the event to the event handler, is stored in the Designer.cs file for the form.  If necessary, you can open this file to view or delete the event wiring.

  • In chapter 6, you'll learn how to handle events other than the default event.

That's because when you create an event handler, Visual Studio also generates a statement that wires the event to the event handler. As a result, if you delete an event handler, you must also delete the wiring for the event. The easiest way to do that is to double-click on the error message in the Error List window. This will open the Designer.cs file for the form and jump to the statement that contains the wiring for the missing event handler. Then, you can delete this statement.

Total Pages : 10 12345

comments