19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

■ EVENTS<br />

In the previous section we drew a parallel between objects <strong>and</strong> controls <strong>and</strong> showed how to define<br />

properties <strong>and</strong> methods for classes. In addition to the two predefined events for classes,<br />

Initialize <strong>and</strong> Terminate, other events can be defined by the programmer to communicate<br />

changes of properties, errors, <strong>and</strong> the progress of lengthy operations. Such events are called<br />

user-defined events. The statement for triggering an event is located in the class module <strong>and</strong><br />

the event is dealt with in the form code. Suppose the event is named UserDefinedEvent <strong>and</strong><br />

has the arguments arg1, arg2, <strong>and</strong> so on. In the class module, the statement<br />

Public Event UserDefinedEvent(arg1, arg2, ...)<br />

should be placed in the (Declarations) section of (General), <strong>and</strong> the statement<br />

RaiseEvent UserDefinedEvent(arg1, arg2, ...)<br />

should be placed at the locations in the class module code at which the event should be triggered.<br />

In the form code, an instance of the class, call it object1, must be declared with a statement<br />

of the type<br />

Private WithEvents object1 As CClassName<br />

in order to be able to respond to the event. That is, the keyword WithEvents must be inserted<br />

into the st<strong>and</strong>ard declaration statement. The header of an event procedure for object1 will be<br />

Private Sub object1_UserDefinedEvent(par1, par2, ...)<br />

EXAMPLE 3<br />

Consider the circle class defined in Example 3 of Section 12.1. Add a user-defined event that is triggered<br />

whenever the center of a circle changes. The event should have parameters to pass the center <strong>and</strong> radius<br />

of the circle. The form code should use the event to determine if part (or all) of the drawn circle will fall<br />

outside the form. If so, the event procedure should display the message “Circle Off Screen” in a label <strong>and</strong><br />

cause all future circles to be drawn in red.<br />

SOLUTION:<br />

Let’s call the event PositionChanged.<br />

Object Property Setting<br />

frmCircles BorderStyle 0-None<br />

cmdMove Caption Move <strong>and</strong> Show<br />

Circle<br />

lblCaution BorderStyle 1-Fixed Single<br />

Caption, (blank)<br />

cmdQuit Caption, Quit<br />

‘Class module for CCircle<br />

Private m_x As Integer ‘Dist from center of circle to left side of form<br />

Private m_y As Integer ‘Distance from center of circle to top of form<br />

Private m_r As Single ‘Radius of circle<br />

Public Event PositionChanged(x As Integer, y as Integer, r As Single)<br />

’Event is triggered by a change in the center of the circle<br />

Private Sub Class_Initialize()<br />

Collections <strong>and</strong> Events 371

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!