Taming the Visual FoxPro Report Preview Window

download Taming the Visual FoxPro Report Preview Window

of 10

Transcript of Taming the Visual FoxPro Report Preview Window

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    1/10

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    2/10

    These are some of the things that users and developers dislike about the default preview window:

    Users frequently click on the preview expecting to select some text, and are disconcerted to findthat this causes the zoom level to change.

    Users navigate the preview via the report toolbar. If the user accidentally closes the toolbar(and users often do), there's no immediate way of getting it back.

    The preview window doesn't consistently remember its size, position or zoom factor (thesesettings are stored in the resource file, which might not be available at run time).

    You can't easily control the window size, position or zoom factor programmatically.

    In VFP 8.0 and below, there are only five zoom levels, with no whole-page option. This hasincreased to a more useful nine in VFP 9.0, but still without a whole-page view.

    You can only view one page at a time. In some situations, users might prefer to view two pagesside by side (for example, where the report is to be printed as a bound book), or perhaps evenfour pages at a time.

    In this article, we'll suggest ways in which you can improve the default preview window, or replace itwith a custom-built report viewer of your own. Most of the techniques we describe are only availablein VFP 9.0, but we'll start with one that's been around since version 5.0.

    The IN WINDOW option

    VFP's REPORT FORM command includes an IN WINDOW clause that lets you host the preview windowin your own form. This isn't quite as wonderful as it first sounds, but it does open the way to a fewuseful enhancements.

    To take advantage of this feature, first launch a form and make it visible. Adjust its size, position andother properties as necessary. Then execute the report, passing the name of the form (that is, thevalue of its Name property) to the IN WINDOW clause:

    LOCAL loPreview

    loPreview = CREATEOBJECT("Form")

    * At this point, you can customise the form in

    * certain ways, for example by changing its

    * size, position, caption or icon.

    loPreview.Caption = "Report Viewer"

    loPreview.Name = "MyPreview"loPreview.Visible = .T.

    Taming the Visual FoxPro report preview window http://www.ml-consult.co.uk/foxst-46.ht

    2 of 10 02/01/2012 08:57

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    3/10

    REPORT FORM MyReport PREVIEW IN WINDOW MyPreview

    This example uses the built-in form class as the basis for the preview form, but you can use your owncustom class if you prefer. By default, the preview will be modal (regardless of the form's WindowTypeproperty) and also non-resizable. The preview will fill the form, so any other controls on the form will

    be obscured.

    You can change that behaviour by adding NOWAIT to the REPORT FORM command. This will make theform modeless and resizable (in fact, you can resize the preview area independently of the outerform). But keep in mind that when the variable holding the form object (loPreview in this example)goes out of scope, the form will disappear.

    This technique gives you a little more control over the preview window. But, because you don't havean object reference to the preview itself, you have very little scope for customising it.

    VFP 9.0's alternative preview window

    VFP 9.0's default preview window suffers from the same limitations as in earlier versions (apart froma couple of minor improvements, such as the increased number of zoom levels). But did you knowthat it's now possible to invoke an alternative window that overcomes some of those earlier problems?

    The alternative window is based on a report listener. But you don't need to know anything aboutreport listeners to take advantage of it. In fact, you only need to make one small change to your

    code. Instead of this:

    REPORT FORM MyReport PREVIEW

    do this:

    REPORT FORM MyReport OBJECT TYPE 1

    Figure 2 shows the result.

    Taming the Visual FoxPro report preview window http://www.ml-consult.co.uk/foxst-46.ht

    3 of 10 02/01/2012 08:57

    T i th Vi l F P t i i d htt // l lt k/f t 46 ht

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    4/10

    Figure 2: VFP 9.0's alternative preview window offers several improvements.

    The new preview window is broadly similar to the old version, but with these useful improvements:

    More zoom levels, including a whole-page option. (A very minor issue is that the zoom menuappears in ascending order, which is the opposite of the normal Windows behaviour.)

    A context menu is available, as an alternative to the toolbar.

    You can choose to view one, two or four pages at a time (subject to the zoom level).

    Best of all, you don't see the irritating change of zoom level when you click in the preview.

    As before, the preview is modal by default. But, as before, you can use NOWAIT to make it modeless.

    If you have many REPORT FORM commands scattered around your application, you won't need tomodify each one separately. Just execute this command near the start:

    SET REPORTBEHAVIOR 90

    Once you've done that, all REPORT FORM commands containing the old PREVIEW clause will nowinvoke the new preview window. (SET REPORTBEHAVIOR is not scoped to the current data session.)

    Taming the Visual FoxPro report preview window http://www.ml-consult.co.uk/foxst-46.ht

    4 of 10 02/01/2012 08:57

    Taming the Visual FoxPro report preview window http://www ml consult co uk/foxst 46 ht

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    5/10

    Going further with the report listener

    Because this alternative preview window is based on a report listener, you can do a certain amount ofcustomisation just by adjusting the listener's properties. Consider the following code:

    LOCAL loPreview

    loPreview = NULL

    DO (_REPORTPREVIEW) WITH loPreview

    The _REPORTPREVIEW system variable points to a report previewing application. By default, thatapplication is ReportPreview.APP. Given enough time and know-how, you could create your ownreport previewing application to use in place of ReportPreview.APP. We'll discuss that possibility laterin the article. For now, let's keep things simple.

    The third line in the above code calls ReportPreview.APP (or any replacement for it), passing a NULLobject parameter by reference. ReportPreview.APP's job is to create a container object that will holdthe preview. It uses the parameter to return a reference to the container.

    Having obtained the reference (loPreview in this example), you can access the container's properties,for example:

    loPreview.CanvasCount = 2

    loPreview.ZoomLevel = 4 && 75%loPreview.Width = 800

    loPreview.ToolbarIsVisible = .F.

    Here, we're setting the initial canvas count (the number of pages displayed at one time) to two, theinitial zoom level to 75% (the fourth item on the zoom menu) and the width of the preview to 800pixels; we are also hiding the toolbar.

    At this stage, there's nothing visible on the screen. In fact, the report hasn't even been rendered yet.

    So the next step is to instantiate a report listener object and to point it to our preview container:

    loListener = CREATEOBJECT("ReportListener")

    loListener.PreviewContainer = loPreview

    lolistener.ListenerType = 1

    We're using the built-in ReportListener class here, but we could just as well have used a subclass of it.Many developers prefer to use the _ReportListener foundation class (note the underscore at the startof the name), which offers a number of additional features.

    Setting the listener's ListenerType property to 1 is important. This tells the listener to render thepages and to pass them to the preview object. If you left this property at its default setting of -1, the

    Taming the Visual FoxPro report preview window http://www.ml-consult.co.uk/foxst-46.ht

    5 of 10 02/01/2012 08:57

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    6/10

    Taming the Visual FoxPro report preview window http://www ml-consult co uk/foxst-46 ht

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    7/10

    Figure 3: The FRXPreviewForm class as it appears in the class designer. The four large shapes represent the canvases

    on which the preview appears. The smaller shape is a spacer.

    As you will see, this class contains all the methods for rendering, navigating, and generally managingthe preview. For example, there are methods called ActionGoFirst, ActionPrint, ActionSetCanvasCountand ActionSetZoom, all of which do what their names suggest. You can use this code as the basis foryour own customised report viewer, which you can then embellish in whatever way suits yourapplication.

    Figure 4 shows our own generic preview form. This contains code from the class mentioned above as

    well as a little custom code of our own. Our main aim was to place all the required controls - pagenavigation, zoom, canvas count, etc. - on the form itself, and thus avoid the user having to rely onthe context menu or toolbar. The preview container is in fact the scrolling region that occupies themain part of the form. To create that scrolling region, we used the techniques described in theFoxStuff article, Create a scrolling region within a form. (A significant drawback of that technique isthat it will only work if the form is modeless.)

    Taming the Visual FoxPro report preview window http://www.ml consult.co.uk/foxst 46.ht

    7 of 10 02/01/2012 08:57

    Taming the Visual FoxPro report preview window http://www.ml-consult.co.uk/foxst-46.ht

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    8/10

    Figure 4: A custom report viewer which incorporates the FRXPreviewForm class. The controls are all on the form itelfrather than in a toolbar.

    Because most of the code in the form is Microsoft's copyright, we can't make the form available foryou to download, nor can we show you the code. However, by using the existing source code as astarting point, you should be able to create your own preview form in much the same way that wehave done.

    The third-party option

    If all this sounds like too much trouble, you have another option - one that involves very littleprogramming. There are at least two third-party products available for purchase that you can use toeasily create a custom report viewer. They are:

    FRX2ANY, from Semurg Enterprises

    XFRX, from Eqeus

    We don't have any recent experience of FRX2ANY, but we have used XFRX on a recent project, withsatisfactory results. Figure 5 shows the report viewer that we created with the help of XFRX.

    g p p p

    8 of 10 02/01/2012 08:57

    Taming the Visual FoxPro report preview window http://www.ml-consult.co.uk/foxst-46.ht

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    9/10

    Figure 5: A custom report viewer created with the help of XFRX.

    This form is similar to the preview window shown in Figure 4, but with some additional features.These include a Find button that lets the user search for any text anywhere in the report. With alarge report, the search will be slow, but nevertheless this is a feature that users greatly appreciate.There are also buttons for exporting the report to PDF or Microsoft Word format. The search andexport features are both provided by XFRX, and require very little additional programming. Althoughnot shown here, XFRX also lets you create hyperlinks and bookmarks, and it supports many additionalexport formats.

    We hope that this article has demonstrated that you don't need to put up with the drawbacks of VisualFoxPro's built-in preview window. Whether you tweak the built-in window, create your own viewer, oruse a third-party product, you'll end up with a better way of showing your reports on the screen -something your users are sure to thank you for.

    Mike Lewis Consultants Ltd. December 2007.

    g p p p

    9 of 10 02/01/2012 08:57

    Taming the Visual FoxPro report preview window http://www.ml-consult.co.uk/foxst-46.ht

  • 8/2/2019 Taming the Visual FoxPro Report Preview Window

    10/10

    More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact

    us

    FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass aroundcopies of the articles (but please do not remove our copyright notices or disclaimers).

    The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code,components or techniques unless you are satisfied that they will work correctly in your applications.

    Copyright Mike Lewis Consultants Ltd.

    10 of 10 02/01/2012 08:57