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

Got this from this webpage

How to Code and Test a Windows Forms Application - How to add code to a form

 
(Page 4 of 4 )

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 Visual Basic 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 event of a form or control. 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. When you do that, Visual Studioopens the Code Editor, generates a procedure declaration for the default event of the object, and places the insertion point between the Sub and End Sub statements that it has generated. Then, you can enter the Visual Basic statements for the procedure between the Sub and End Sub statements.

To illustrate, figure 3-4 shows the Sub and End Sub statements that were generated when I double-clicked the Calculate button on the Invoice Total form. In the Sub statement, Visual Studio generated a procedure name that consists of the name of the object that the event occurred on (btnCalculate), an underscore, and the name of the event (Click).

This procedure name is followed by two arguments in parentheses that you'll learn more about later. And the arguments are followed by a Handles clause that says that the procedure is designed to handle the Click event of the button named btnCalculate. It is this clause, not the procedure name, that determines what event the procedure handles.

For now, you should avoid modifying the procedure declaration that's generated for you when you create an event handler. In chapter 6, though, you'll learn how to modify the declaration so a single procedure can provide for more than one event.

The procedure that handles the Click event of the Calculate button

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 procedure that handles the event, and places the cursor within this declaration. 
  2. Type the Visual Basic code between the Sub statement and the End Sub statement. 
     
  3. When you finish entering the 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

  1. The procedure declaration for the event handler that's generated when you double-click on an object in the Form Designer includes a procedure name that consists of the object name, an underscore, and the event name. 
  2. The Handles clause in the procedure declaration determines what event the procedure handles using the object name, dot, event name syntax. 
     
  3. In chapter 6, you'll learn how to handle events other than the default event. 

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

 

  • Please check back tomorrow for the continuation of this article.

     

  • No comments: