The SetAtRef ShapeSheet function creates a cell dependency just as we saw in the DependsOn example, but it gives greater control over what action is to be taken when the cell change occurs.
Demonstration: SetAtRef -
From your student CD, open \Demos\Vol3\Managing Events\SetAtRef Example.vsd. Note that there are 3 examples on 3 separate pages in the drawing.
-
Example 1
-
Choose drawing page SetAtRef
-
Display the shape data window. Data >Shape Data Window.
-
This example is similar to the DependOn example we looked at earlier except that the cells being controlled are the Width and Height cells. Modify the shape’s Width or Height by using any of the UI tools or by modifying the shape data fields.
-
Note the SetAtRef formulas in the shape’s Width and Height cells. The SetAtRef function redirects any changes to the Width and Height cells to go to the corresponding shape data cells.
-
Example 2
-
Choose drawing page SetAtRefExpr
-
This example uses SetAtRefExpr to force the shape to snap to a custom grid, in this case a 1 inch by 1 inch grid. View the ShapeSheet to see the formulas.
-
Example 3
-
Choose drawing page Associate without glueing
-
The ShapeSheet formulas are in the Child shape’s PinX and PinY cells. Example shows use of SetAtRef, SetAtRefExpr, and SetAtRefEval together to create a shape that snaps to a fixed grid size. The grid size is defined in User cells.
Use the ShapeSheet to Manage Events
The Event section cells of the ShapeSheet provide the next level of event management for the Visio developer. These cells can be used to program events triggered when:
-
The text is changed (TheText)
-
The shape is double-clicked (EventDblClick)
-
The shape’s position, size, or orientation on the page is changed (EventXFMod)
-
The shape is dropped (EventDrop)
-
Multiple shapes are dropped on the page at the same time (EventMultiDrop)
You will see an Event cell called TheData. This currently does nothing and has been in the product since its earliest days.
This completes our discussion of handling events through the ShapeSheet. As you have seen several times in this class, putting capabilities into the ShapeSheet is preferred to writing code. The ShapeSheet offers a rich set of features and functions that make the development of code behind events unnecessary. Shapes are easily transferred when drawing files are exchanged.
However, if you need a more robust and expandable solution, writing event handling code in an add-on may be easier to maintain. But remember - shapes are part of the Visio drawing file, while add-ons are not!
Visual Basic for Applications Code Behind Events
Objects have events defined for them, such as the ShapeAdded event when a shape is added to a Visio document and the Click event of a button on a form when the button is pressed.
Take a look at how Visual Basic and Visual Basic for Applications have helped programmers set up event handlers.
Always keep in mind that Visual Basic for Applications uses the underlying Event Object Model defined in the Visio Type Library to respond to events. It utilizes this event model to allow you to put code behind events.
Supported Events
There are many supported automation events in Visio. Certain events are only available on particular source objects. For example, you can only respond to the AfterModal event on an Application source object.
One key point about the Before events is that they are just notifications that the event is about to happen. You cannot programmatically stop the event from occurring.
For example, when the BeforeSelectionDelete event is triggered, the selection passed to the event handler is committed to be deleted. You cannot stop the deletion.
Hierarchical Event Sets
Some events, such as the ShapeAdded event, can be sourced by different objects. The ShapeAdded event can be detected at the page level, at the document level, or at the application level.
The number of events your code will process can be very different depending upon which object you choose as the source object for your event. For example, at the application level, the ShapeAdded event will fire any time a shape is added to any page in any open drawing in the application.
If the page object is used as the source object, the event fires only when a shape is added to that page, but not when a shape is added to any other page in the application.
You can determine which objects source an event by using either the Object Browser in Visual Basic for Applications or the Developer Reference help from the Visio Help menu.
Built-In Document Events
Like other Microsoft Office applications that contain Visual Basic for Applications, Visio has implemented a class known as ThisDocument.
A large number of events have already been provided, which means you have the ability to use the Visual Basic code framework and write your own procedures associated with any of these events.
Try it! Add Code Behind Events for ThisDocument -
In a blank drawing, open the Visual Basic for Applications code window.
-
In the Project Window, right-click ThisDocument and choose View Code.
-
In the code window for ThisDocument, in the left-hand drop down list box, choose Document.
-
In the right-hand drop down list box, choose ShapeAdded.
-
The Document_ShapeAdded event procedure is created. Add the code:
Msgbox “Shape added to the document”
-
Go back to the Visio drawing and add a shape to the page. This will trigger the ShapeAdded event and execute the Msgbox statement above.
Share with your friends: |