Processing Events

23.04.2021

This example shows how you can capture and respond to events in your Basic programs. The program code and the required FlexPro objects can be found in the database called Events.fpd. The path to the Project Database is usually C:\Users\Public\Documents\Weisang\FlexPro\2021\Examples\VBA\Events\Events.fpd or C:>Users>Public>Public Documents>Weisang>FlexPro>2021>Examples>VBA>Events>Events.fpd.This database contains a diagram with two X axes. The X values of the curve are scaled above the lower axis. In the example, the curve represents a route measured in km. The second axis, which is shown at the top of the diagram, assigns names to certain locations along this route. In the example, these are names of cities that can be found on the route. Two data series were created to scale this axis. The Cities data series contains the names of the cities, and the Positions data series contains their locations along the route. To label the axis with this data, the Linear, division by data set axis type was selected on the Axis Scaling tab and the Positions data series was entered as a data set. This determines the points at which axis divisions are to appear. On the Division Labeling tab, the option Division labeling using a data series, signal or function was enabled and Cities was entered as a data set. Since FlexPro 7 you have been able to easily synchronize the axis end values of this second X axis with the first on the Axis Scaling by selecting the option Same as previous axis. This axis cannot be auto-scaled because no curve will be shown above it.

The following example, which was originally developed for FlexPro 6, illustrates how the end values of the second axis can be synchronized with the first using event processing. The following requirements result:

Event processing must be activated when you open the database and reset to its former state when you close the database.

When opening the diagram, the second axis is to be set to fixed scaling and the current end values of the first axis are to be adopted.

When zooming in or out using the cursor, the new end values of the first X axis are to be adopted by the second.

Activating Event Processing

This task can be executed by implementing the two auto macros AutoOpen and AutoClose. The AutoMacros module contains the following code:

Private bEnableEvents As Boolean

 

Sub AutoOpen()

    bEnableEvents = Application.EnableEvents

    Application.EnableEvents = True

End Sub

 

Sub AutoClose()

    Application.EnableEvents = bEnableEvents

End Sub

 

AutoOpen stores the current status of the application object's EnableEvents property and then event handling is enabled.

The previous state is restored in AutoClose.

Initializing Axis Scaling

This task is executed by an event procedure that handles the BeforeOpenObject event, which is sent by every FlexPro object before opening it. In the VbaProject of Events.fpd there is a corresponding object called AnyFpObject which represents all FlexPro objects of the database and therefore receives all events common to all FlexPro objects. The following code is assigned to this class object:

Private Sub FpObjectClass_BeforeOpenObject(ByVal Object As Object)

    Dim oDiag As Diagram2D

    If Object.FullName = "\Route.2D" Then

        Set oDiag = Object

        oDiag.XAxes(2).Scaling.ModeStartValue = fpAxisScalingModeFixed

        oDiag.XAxes(2).Scaling.ModeEndValue = fpAxisScalingModeFixed

        oDiag.XAxes(2).Scaling.StartValue = oDiag.XAxes(1).Scaling.CurrentStartValue

        oDiag.XAxes(2).Scaling.EndValue = oDiag.XAxes(1).Scaling.CurrentEndValue

    End If

End Sub

 

The If test verifies whether the correct 2D diagram has been selected. The name, database path and the file name extension for the 2D diagrams have to match. The event procedure receives this event when any database object, e.g. another 2D diagram or data set, is opened. Within the If test, it is clear that we are dealing with a 2D diagram. The undefined object can therefore be assigned to a variable of the Diagram2D type. The next steps are easy to understand. At first, the starting value and the end value are set to fixed scaling. After that, the current starting and end values of the first axis are adopted. The properties CurrentStartValue and CurrentEndValue provide the values resulting from auto-scaling and not those that have been entered on the Axis Scaling tab of the axis, which are inactive in this case.

Adopting the Axis End Values After Zoom Operations

Here, a multitude of events have to be handled, since several operations affect the axis end values. You can zoom in between the cursors, zoom in on a rectangular area or scroll along the axis. All these events only apply to cursor objects, i.e. objects that may contain cursors, which are diagrams, worksheets and documents. The AnyCursorObject class object, which receives all cursor events, represents all cursor objects. It contains the following code:

Private Sub AdaptScaling(Object As CursorObject)

    Dim oDiag As Diagram2D

    If Object.FullName = "\Route.2D" Then

        Set oDiag = Object

        oDiag.XAxes(2).Scaling.StartValue = oDiag.XAxes(1).Scaling.CurrentStartValue

        oDiag.XAxes(2).Scaling.EndValue = oDiag.XAxes(1).Scaling.CurrentEndValue

    End If

End Sub

 

Private Sub CursorObjectClass_AxisScaled(ByVal CursorObject As Object)

    AdaptScaling CursorObject

End Sub

 

Private Sub CursorObjectClass_AxisScrolled(ByVal CursorObject As Object)

    AdaptScaling CursorObject

End Sub

 

Private Sub CursorObjectClass_RectangleZoomed(ByVal CursorObject As Object)

    AdaptScaling CursorObject

End Sub

 

Private Sub CursorObjectClass_ZoomedBetweenCursors(ByVal CursorObject As Object)

    AdaptScaling CursorObject

End Sub

 

Private Sub CursorObjectClass_ZoomReset(ByVal CursorObject As Object)

    AdaptScaling CursorObject

End Sub

 

Private Sub CursorObjectClass_ZoomUndone(ByVal CursorObject As Object)

    AdaptScaling CursorObject

End Sub

AdaptScaling is an auxiliary procedure called by the different event procedures. Again, the procedure first verifies that the object to be processed does exist. The new axis end values are then copied. All event procedures with events that have an impact on axis scaling were implemented. And all events that are sent after the corresponding operation were selected so that the axis end values of the first X axis already feature the new values.

Share article or send as email:

You might be interested in these articles