Event Example 2: Modify Object Properties on Triggered Event

This exercise follows the above exercise, you should have completed it first. This example also uses the Form Editor.

Suppose you want to modify the properties of another object (say the text in a text object) when the button has been clicked.

  1. In the Form Editor, switch back to edit mode (press Ctrl + E).

  2. Create a new text object.

  3. Open the text object’s properties (select and then right mouse click).

  4. Make sure that the text item’s Object Code property reads "Text1". The Object Code property is the name that is used to refer to that object from BASIC code.

  5. If not expanded, click on the + sign next to the Events section to expand the list.

  6. Double-click on the EventInitialize handler function. The VBS Mini-Editor opens.

  7. Add the following code to the EventInitialize function:
    Set This = Text1

  8. Press the Enter key to get to a new line. On the next line, type this code:
    This.Text = "The button has not yet been clicked"

  9. Click the OK button to close the editor.

  10. Once the object Text1 has been set up as described, open the property sheet for the button object added in the prior exercise.

  11. Click the Events tab.

  12. Double-click on the EventClick function.

  13. Replace the basic code you added in the previous example with the following two lines:
    Line 1: MsgBox "I’ve been clicked, and am about to modify Text1’s text"

    Line 2: Text1.Text = "The button was clicked."

  14. Click the OK button to close the VBS Mini-Editor.

  15. Close the Property Sheet.

  16. Switch to run mode (press Ctrl + E). The code you added to Text1’s EventInitialize function sets Text1 to initially read "The button has not yet been clicked".

  17. Now try to click the button. The "I’ve been clicked, and am about to modify Text1’s text" message should appear. As soon as you close the message box, Text1’s text should be modified to read "The button was clicked.".

This example illustrates how the properties of any object in a form may be accessed from Basic code via the ObjectCode.property_name syntax.