Volume 3: Programming with the Visio Object Model disclaimer



Download 0.78 Mb.
Page20/36
Date29.01.2017
Size0.78 Mb.
#11974
1   ...   16   17   18   19   20   21   22   23   ...   36

WithEvents Keyword


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

  1. In a blank drawing, open the Visual Basic for Applications code window.

  2. 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.



  1. In the Document_RunModeEntered event procedure, add the code:

Set oPage = Visio.ActivePage

  1. In the oPage_ShapeAdded event procedure, add the code:

Msgbox “A Shape was added to Page: ” & oPage.Name

  1. Click the Design Mode button twice to toggle from Design to Run mode, and back.

  2. 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

  1. Open a Blank Visio drawing.

  2. From the Developer tab, choose Insert and choose a CommandButton.

  3. Move and size the control as needed. You can do this, as with any shape, by using the selection handles.

  4. 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.

  5. 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!”

  1. 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

  1. Open \Demos\Vol3\Managing Events\Query Events.vsd

  2. Select one of the shapes and click the command button.

This calls a simple Selection.Delete method, and the BeforeSelectionDelete and BeforeShapeDelete fire.

  1. From the Quick Access Toolbar, choose Undo (or use the shortcut, Ctrl-Z).

  2. Select another shape on the page, and press the delete key on your keyboard.

This time, the QueryCancelSelectionDelete event is called.

  1. Click the Yes button on the message box, to cancel the deletion.

  2. Repeat step 5, but choose the No button on the message box for a different result.

  3. 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.


Download 0.78 Mb.

Share with your friends:
1   ...   16   17   18   19   20   21   22   23   ...   36




The database is protected by copyright ©ininet.org 2024
send message

    Main page