Partilhar via


Dev Luv: Four Best Practices When Event Handling in Visio

1. Avoid Holding onto Multiple Visio Objects: If you need to keep track of Visio, pages, documents or shapes, use IDs instead of objects. A given shape can always be associated with a tuple <document ID, page ID, shape GUID>.

2. Use AddAdvise/IVisEventSink Event Handling: AddAdvise/IVisEventSink is more efficient than using IConnectionPoint since the solution client is only notified for the events that are registered for the solution client via Add-Advise. In addition, more events are available from source objects via AddAdvise. This is especially true on the application and document objects that exist by default in a VBA Project.

3. Listen to Events Sourced by Document and Application Objects Only: You can focus your listening to most events on the document object and any application events that are not already supported on the document. Most Visio solutions are built around a drawing type and associated template that supports the particular drawing type. Users expect to be able to create a drawing of the same type or continue an existing drawing across multiple pages in a document. As a result, you typically want the same behavior throughout the document and need to listen to drawing-related events on the document rather than shapes or pages in the document. Also, listening to document and application events helps limit number of Visio objects that you need to keep as references.

4. Persist Events to Call Your Solution in the Template (if Using a Template) : You can call your solution on DocumentOpen and DocumentCreate by persisting these events in your solution template. As of Visio 2002 SR1, you can persist events in the document for calling an add-in as well as add-on by adding a persistent event that makes a call to the QueueMarkerEvent add-on (see Chris Castillo’s blog entry on QueueMarker for more details). Persisting an event in the document is better than using VBA since it’s harder for the typical user to modify. You can use the Persist Event tool that ships in the Visio 2002 SDK and the Visio 2003 SDK.

 

-- Mai-lan

 

This posting is provided "AS IS" with no warranties, and confers no rights