Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Event Binding in VFP 8 : Page 2

For most applications, events are what drive the development effort, but event handling doesn't offer developers much flexibility. But in VFP 8, event handling is changing for the better.


advertisement

Manual Event Binding
So let's assume we have the following method as a member of our form, and would like to bind it to a button on our form:

   FUNCTION ShowMessage()
      MessageBox("Button Clicked")
   ENDFUNC
We could do so by using the new BindEvent() function. We could use that function whenever and wherever we want. In this example, the form's Init() method might be the best place:

   FUNCTION Init()
      BINDEVENT(THIS.command1,"Click",;
         THIS,"ShowMessage")
   ENDFUNC
The first two parameters define the source of the event (object, plus name of the event), the second pair defines the handling object and the method we want to delegate the event to.

If you run this code inside a form and click the button, you will see the message box firing. Note that this doesn't influence the original click event at all. You can check this by simply adding another message box to the button's Click() method. In that case, the ShowMessage() method will fire first (whenever a Click event is raised), and then the Click() method will follow.

If you would like the sequence to occur the other way around, with Click() firing first, you can indicate so by passing an optional 5th parameter to the BindEvent() function:



   BINDEVENT(THIS.command1,"Click",;
      THIS,"ShowMessage",1)
Aside from the sequencing, what is interesting here is that we can have more than one handler for an event. In this case, we have our ShowMessage() method as well as the default Click() method. And it doesn't stop there! In fact, we can add as many different event handlers to each event as we want!

Unbinding Events
Just like we bound event handlers to events, we can also unbind events. So let's assume we want our ShowMethod() to only handle the very first Click event the button raises, but not subsequent ones. In that case, we could add the following code to the ShowMessage() method:

   FUNCTION ShowMessage()
      MessageBox("Button Clicked")
      UnbindEvents(THIS.command1,"Click",;
         THIS,"ShowMessage")
   ENDFUNC
Of course, this assumes that the ShowMessage() method is designed to be attached to THIS.command1, and no other object. In real-life scenarios, you may not want to do that, because these types of dynamic event handlers generally are very generic so they can be attached to different objects. However, it would be easy enough to put this code in other methods or event handlers as well. We will also explore some generic ways to discover current bindings a later in this article.

Note that there also is a simpler version of UnbindEvents() that receives one parameter (an object reference):

   UnbindEvents(THIS)
This will automatically unbind all event handlers on the THIS-object (which would include our ShowMessage() method). In addition (and this is important to know), it will also unbind all event handlers in other objects, that are binding to events that may occur on the THIS-object. Therefore, if another object binds to the Activate event of the form (for instance), that binding will be released as well. While this is useful in some scenarios (perhaps we want to release the THIS-object from memory), it is also a rather barbaric approach, compared to the surgical accuracy of the 4-parameter version.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date