Handling Preview Window Events

The Report and Window objects in the Crystal Report Engine Object Library include several Events. By handling these events in your Visual Basic project, you can customize how your application responds to user actions. For instance, if a user clicks on a button in the toolbar of the preview window, such as the Zoom button or the Next Page button, your application can respond to that event.

Note:    Events are only available in Visual Basic 5.0 and later. If you are using a version of Visual Basic earlier than 5.0, you will not be able to make use of the Events exposed by the Report or Window object.

To handle Events for the Report or Window object, you must declare the instance of the object as Public and WithEvents. For example:

Public WithEvents repEvents As CRPEAuto.Report
Public WithEvents wndEvents As CRPEAuto.Window

Once declared, the objects will appear in the Visual Basic Object window. If you select the object, its Events will be displayed, just as if you were working with any other Visual Basic object.

Note:    The Window object events are only valid when a report is sent to a preview window using the Preview method.

The following code demonstrates how to set up and use events for both the Report object and the Window object. Actual event handling code is left for you to fill in. You are limited only by the restrictions of the Visual Basic language.

Option Explicit
Public WithEvents rpt1 As CRPEAuto.Report
Public vw1 As CRPEAuto.View
Public WithEvents wnd1 As CRPEAuto.Window Private Sub Command1_Click()
    Set vw1 = rpt1.Preview
    Set wnd1 = vw1.Parent
End Sub Private Sub Form_Load()
    Set app1 = CreateObject("Crystal.CRPE.Application")
    Set rpt1 = app1.OpenReport("c:\crw\rt01.rpt")     rpt1.EventInfo.ActivatePrintWindowEventEnabled = True
    rpt1.EventInfo.ClosePrintWindowEventEnabled = True
    rpt1.EventInfo.GroupEventEnabled = True
    rpt1.EventInfo.PrintWindowButtonEventEnabled = True
    rpt1.EventInfo.ReadingRecordEventEnabled = True
    rpt1.EventInfo.StartStopEventEnabled = True End Sub Private Sub rpt1_Start(ByVal Destination As _
                CRPEAuto.CRPrintingDestination, _
                useDefault As Boolean)     ' Put event handling code here. End Sub Private Sub rpt1_Stop (ByVal Destination As
                CRPEAuto.CRPrintingDestination,
                ByVal Status As CRPEAuto.CRPrintingProgress)     ' Put event handling code here. End Sub Private Sub wnd1_ActivatePrintWindow() ' Put event handling code here. End Sub Private Sub wnd1_ClosePrintWindow (useDefault As Boolean) ' Put event handling code here. End Sub ' Other events for the Report and Window objects
' can be seen by using the Visual Basic Object Browser
' with the Crystal Report Engine Object Library.
' (a lightning bolt icon appears next to Events in
' the Object Browser.) ' Once and instance of a Report object or Window object,
' is declared, you can add Event handlers to your code by
' selecting the object in the Visual Basic Object list and
' then selecting the desired event.

For complete descriptions of all available Crystal Report Engine Object Library Events, refer to the Report Object and the Window Object.

Note:    In the previous version of Crystal Reports a report or window object variable declared WithEvents could only be Set once. A VB error occurred if you tried to Set the variable to a different value(i.e. access a new report or display a new Preview window). This problem no longer exists. You can now reset the values of WithEvent object variables.



Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com