Using Events to Control Actions

Microsoft FrontPage Visual Basic

Using Events to Control Actions

There are two types of events in Microsoft FrontPage— events that are raise from the Application and Web object models and events that are raised from the Page object model.

Application events

The events for the Application object model can be used to control under what conditions a Web site is published, whether you want to save a page whenever the OnPageClose event is fired, or whether you want to set styles, fonts, or backgrounds whenever a new page is created.

Page events

In the Microsoft Internet Explorer DHTML object model, event handlers are created using scripts for use at run-time.

However, in the FrontPage Page object model, you're programming with events and objects that are compatible with Internet Explorer, but for use at design time. To program events for runtime, you can use the standard Microsoft Visual Basic 5.0 (or higher) keywords to access the Page object model events just as you would to access the Web object model events. This method combines the two techniques described previously. The following example catches the onclick event for a hyperlink in FrontPage.

In the Visual Basic Editor, insert a class module and name it CatchOnClick. Add the following code to the class module.

Dim WithEvents eAnchor As FPHTMLAnchorElement
Dim WithEvents eDoc As FPHTMLDocument
Dim e As IHTMLEventObj

Private Sub Class_Initialize()
Set eDoc = ActiveDocument
Set eAnchor = eDoc.links(0)
End Sub

Private Function eAnchor_onclick() As Boolean
Set e = eAnchor.Document.parentWindow.event
If (MsgBox("OnClick Event for " & e.srcElement.tagName & _
    " would you like to cancel the event bubbling?", _
    vbYesNo) = vbYes) Then
    e.cancelBubble = True
    e.returnValue = False
Else
    e.cancelBubble = False
    e.returnValue = True
End If
End Function

Private Function eDoc_onclick() As Boolean
MsgBox "OnClick event for the Document object"
End Function

Next add a standard module and add the following code.

Public e As CatchOnClick
Sub GetClick()
Set e = New CatchOnClick
End Sub

Note  To run the example, perform the following steps:

  • Add a hyperlink to a page in FrontPage.
  • Run the GetClick procedure to create a global instance of the CatchOnClick event handler class.
  • Click the hyperlink.

A prompt is displayed stating that the onclick event fired. The prompt also queries the user to find out whether the program should pass the event on up the event chain. If Yes is chosen, the onclick event is passed up to the document object to be handled.

To control which document object the event is passed to, you must set both the cancelBubble and returnValue properties. The cancelBubble event works to cancel the event from going any farther up the event chain. Set the cancelBubble property of the IHTMLEventObject to True when you don't want the onclick event to be passed up to the next level of onclick events, otherwise, set the cancelBubble property to False. For example, if you have an image that has an onclick event placed on a document, which also has an onclick event, you would set the cancelBubble property for the IHTMLEventObj object to True for the image, if you don't want the onclick event to be passed on up to the document onclick event.

The returnValue property is used to control the default action taken by FrontPage when an event fires. Using the previous example of an image placed on a document, if the returnValue property for the IHTMLEventObject for the image is set to False in the onclick event, then the shortcut menu would be disabled (because the right-click context menu is the default action for the onclick event).