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

Got this from this webpage

How to Code and Test a Windows Forms Application - How an application responds to events

 
(Page 3 of 4 )

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 procedure known as an event handler. In figure 3-3, you can see the event handler for the event that occurs when the user clicks the Exit button on the Invoice Total form. In this case, 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 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

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

  Private Sub btnExit_Click(ByVal sender As System.Object,
          ByVal e As System.EventArgs) Handles btnExit.Click
      Me.Close()
  End Sub

Common control events  

 

Event

Occurs when…

Click

…the user clicks the control.

DoubleClick

…the user double-clicks the control.

Enter

…the focus is moved to the control.

Leave

…the focus is moved from the control.

 

 

Common form events  

 

Event

Occurs when…

Load

…the form is loaded into memory.

Closing

…the form is closing.

Closed

…the form is closed.

 

Concepts

  1. Windows Forms applications work by responding to events that occur on objects. 
  2. To indicate how an application should respond to an event, you code an event handler, which is a Visual Basic procedure that handles the event. 
     
  3. 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. 

Figure 3-3.   How an application responds to events

No comments: