Visual Basic for Applications supports the keyword WithEvents that allows you to set up event handling for any source object.
Use the Visual Basic for Applications keyword WithEvents to declare an object variable for the Visio object whose events you want to handle. For example:
Dim WithEvents oPage as Visio.Page
In addition to the usual access to the page object’s properties and methods, this declaration gives the oPage variable the capacity to handle events when bound to a particular instance of a page. Assigning Visio.ActivePage to oPage enables oPage to source events for the page represented by Visio.ActivePage at the time of the binding.
Demonstration: WithEvents -
In a blank drawing, open the Visual Basic for Applications code window.
-
In the General Declarations section of ThisDocument, add the code:
Dim WithEvents oPage as Visio.Page
Note that oPage is added to the left-hand drop down list box in the code window and you now have access to the events that a page object can source.
-
In the Document_RunModeEntered event procedure, add the code:
Set oPage = Visio.ActivePage
-
In the oPage_ShapeAdded event procedure, add the code:
Msgbox “A Shape was added to Page: ” & oPage.Name
-
Click the Design Mode button twice to toggle from Design to Run mode, and back.
-
Add shapes to the new drawing page. The message box should be displayed each time a shape is added to the page.
Controls
You do have the ability to add controls to your Visio drawing. Visio has a Design Mode and a Run Mode to allow you to work with the controls. In Design Mode you can place controls in the drawing and set their properties. The drawing must be in Run Mode for the control’s events to fire and allow the user to interact with the controls as she would in a typical Windows application.
Try it! Add a Control to a Visio Drawing -
Open a Blank Visio drawing.
-
From the Developer tab, choose Insert and choose a CommandButton.
-
Move and size the control as needed. You can do this, as with any shape, by using the selection handles.
-
Edit the control by setting its properties. You can do this by right-clicking and choosing CommandButton_Object > Control Properties, or by editing the control’s properties from the VBA properties window.
-
In the Click event for the command button, add the following code. Note that the name of the control has been added to the code window in the left-hand drop down list box.
MsgBox “I am a live control!”
-
Put the drawing in Run Mode (Developer tab and toggle the Developer Mode switch) and click the button.
Query Events
Query events give you a way to cancel the event action before it occurs.
For example, if you have shapes in your custom solution that you do not want the user to delete from the page, you would include the QueryCancelSelectionDelete event in your event sink. Once you sign up for this event, you can add code to respond to it. Then, if a user selects a shape on the page, and presses the delete key to delete it, you can cancel the delete action, and notify the user.
Query events are triggered only by actions from the UI – not through automation.
Here are some of the Query events that Visio provides:
-
QueryCancelConvertToGroup
-
QueryCancelDocumentClose
-
QueryCancelMasterDelete
-
QueryCancelPageDelete
-
QueryCancelSelectionDelete
-
QueryCancelUngroup
-
QueryCancelQuit
-
QueryCancelWindowClose
Query Events (Continued)
This is the event sequence for the QueryCancelSelectionDelete event, as it occurs in Visio.
Note:
|
The Before events fire if the Query event is not cancelled.
| Try it! Query Events -
Open \Demos\Vol3\Managing Events\Query Events.vsd
-
Select one of the shapes and click the command button.
This calls a simple Selection.Delete method, and the BeforeSelectionDelete and BeforeShapeDelete fire.
-
From the Quick Access Toolbar, choose Undo (or use the shortcut, Ctrl-Z).
-
Select another shape on the page, and press the delete key on your keyboard.
This time, the QueryCancelSelectionDelete event is called.
-
Click the Yes button on the message box, to cancel the deletion.
-
Repeat step 5, but choose the No button on the message box for a different result.
-
Open the Visual Basic Editor and review the code.
12.
Marker Events
Marker events may be used to ignore events caused by other events.
Sometimes your event handler modifies the drawing, and this can cause additional events to fire. Was the event caused by user or by some other event handler? This situation can cause loops responding to self-caused events.
The classic example is when the CellChanged event resets the value of cell. This causes another CellChanged event, which may cause the value of the cell to change again. You need to be able to distinguish between the original event and those that are generated by the event manager.
Unlike other events that Visio fires, a client program causes the MarkerEvent to fire by invoking the QueueMarkerEvent method.
The MarkerEvent passes both the context string and sequence number to the MarkerEvent, event handler. Either the context string or sequence number can be used to correlate QueueMarkerEvent calls with MarkerEvent events. This way, your program can distinguish events it caused, from those it did not cause.
For more information on Marker Events, see the sample file, Marker Events.vsd, and search the Visio Developer Reference’s Automation section for the MarkerEvent event and the QueueMarkerEvent method.
Share with your friends: |