Many of the Reflection objects support automation events. Most of these objects appear in the Visual Basic Editor project pane (for example, IbmScreen or ThisScreen).
Key Reflection events occur when:
You can use Reflection events in Reflection macros or in Macros created with other applications. You can also define custom events.
Getting Started With Reflection Events
Using Reflection Events in Sessions Created At Runtime
If you are developing a macro in Reflection, you can use the Visual Basic editor to set up events for a session project.
In the project window, open a code view for one of the default Reflection objects. The top of the code window has has two list boxes with default settings of (General) and (Declarations).
When you change the (General) setting to the object you selected (for our example, we selected ThisView), an event procedure is inserted into the Code window automatically.
You can select other events from the list box on the right. For our example, we'll choose Closing.
In the event procedure you just created for the Closing event, enter the code you want to execute when the event occurs and then save this procedure as part of your session document. The following sample uses the View_Closing event to display a dialog box that requires the user to confirm closing:
Using the Closing event |
Copy Code
|
---|---|
Private Function View_Closing(ByVal sender As Variant) As Boolean If MsgBox("Confirm Close", vbOKCancel) = vbOK Then Debug.Print "True" View_Closing = True Else Debug.Print "False" View_Closing = False End If End Function |
You may need to write macros that start other sessions at runtime. For example, you may need to open a session from an Excel macro or you might need to start a session from a Reflection macro if a specific option is selected by a user. To use events for Reflection sessions started at runtime, you'll need to set up your macro to handle the events.
First, make sure the VBA Project for your Reflection session (or Excel worksheet) has the references for the library you need. If you are in an IBM module and you are adding an Open Systems terminal (or vice versa), you'll need to select the project folder and then add a reference to the library for the terminal you are adding to the project folder.
In our example, we are creating an open systems terminal in an IBM session macro, so we'll need to add a reference to the Reflection library required for Open Systems objects. (The References dialog box is opened from the Tools | References menu.)
Then set up your macro to handle events for the Reflection objects. The default Reflection objects in the Visual Basic editor (such as ThisIbmScreen or ThisScreen) are not available for the new session while you are programming, so you'll need to set up variables for these objects using the VBA WithEvents keyword. The easiest way to declare these variables is to place them in the object module in which you are writing the macro (For example, ThisIbmScreen or ThisScreen for Reflection or ThisWorkbook or Sheet for Excel).
In our example, we declare a public global variable for an Open Systems terminal object in the code window of the ThisIbmTerminal Reflection Object.
Declaring global variable with events in a default class module |
Copy Code
|
---|---|
'Declare this variable in a default object code window Public WithEvents osTerminal As Attachmate_Reflection_Objects_Emulation_OpenSystems.terminal |
When you select the osTerminal on the left selection box, you have access to the events for the new Open Systems control.
After the events are available in the VBA editor, you can select these events and handle them.
Handling events |
Copy Code
|
---|---|
Private Function osScreen_ControlKeySending(ByVal sender As Variant, Key As Attachmate_Reflection_Objects_Emulation_OpenSystems.ControlKeyCode) As Boolean 'Insert code to handle event here End Function |
Using the Reflection DefineEvent method, you can define events that occur when:
This method is a member of the Screen object. You can use it to define an event in a code module procedure or in another event procedure.
This sample defines an event that occurs when there is no host activity for a specified interval of time.
After you have defined the event, you will need to create an event procedure to handle the event. You'll need to use the event number to identify the event (in our sample, we set the event number as 1)
This procedure disconnects from the terminal when the defined event occurs.
Events defined in this way remain defined as long as your Reflection session lasts, and for many events, this is a good solution. But this event is handled to disconnect the session. If you define it in a session module, it will be undefined after it disconnects the session. Since this is a timeout event, it makes since to redefine it when we reconnect. Instead of defining it in a session module, you can define the event in another event, such as the Terminal Object Connect event.
You can also remove events during your session. If you want to define the event for part of your macro and then remove it, you can use the Remove method.