Question
“How do I automatically navigate/scroll once the preview is loaded?”
Solution
With the addition of the scrollable previewer for RB 14, it is increasingly necessary to automatically navigate or scroll to a pre-defined position inside a report.
This can easily be done by using the TppViewer.OnPrintStateChange event and the TppViewer.Busy property.
1. Access the TppViewer object directly or via the TppReport.PreviewForm.Viewer property to assign the necessary event(s). Note: To ensure the PreviewForm.Viewer has been instantiated, access it inside the Report.OnPreviewFromCreate event.
2. Before navigating the report, check that the TppViewer.Busy property is False.
3. Perform the report navigation using the built-in TppViewer routines or by directly accessing the vertical scrollbar and/or horizontal scrollbar.
Viewer Pagination Routines:
– TppViewer.First();
– TppViewer.Last();
– TppViewer.Next();
– TppViewer.Prior();
– TppViewer.GotoPage();
Scrollbar access:
– TppViewer.ScrollablePaintBox.VerticalScrollbar
– TppViewer.ScrollablePaintBox.HorizontalScrollbar
Additional Information:
In some cases, it may be necessary to save the current page number or scroll position for later use.
– Current Page Number
Use an event such as the Report.OnStartPage to save the Report.AbsolutePageNo for later use.
– Current Scroll Position
Use the TppViewer.OnScroll event to save the x or y positions for later use.
Download: PreviewAutoScroll.zip
Sample Delphi code:
procedure TForm1.ppReport1PreviewFormCreate(Sender: TObject);
var
lViewer: TppViewer;
begin
lViewer := TppViewer(ppReport1.PreviewForm.Viewer);
//Assign viewer events after it has been created.
lViewer.OnPrintStateChange := eh_ViewerPrintStateChange;
lViewer.OnScroll := eh_ViewerOnScroll;
end;
procedure TForm1.eh_ViewerOnScroll(Sender: TObject; aX, aY: Integer);
begin
//Keep track of the vertical position
FYPos := aY;
end;
procedure TForm1.eh_ViewerPrintStateChange(Sender: TObject);
var
lViewer: TppViewer;
begin
lViewer := TppViewer(ppReport1.PreviewForm.Viewer);
//Once the viewer is ready, navigate to the saved vertical position
if not(lViewer.Busy) then
lViewer.ScrollablePaintBox.VerticalScrollbar.Position := FYPos;
end;