Adding Buttons to the Viewer Control
This sample project demonstrates how to customize the viewer control by adding additional buttons and icons. After adding these buttons, special functions such as exporting or exiting the project, can be set up to run when the buttons are clicked.
- Start a new Visual Basic standard EXE project.
- Set Form1's properties as follows:
Name frmViewer BorderStyle 1-Fixed Single Height 8310 Width 13440 - Select the ActiveX Viewer Control from VB's component list and add the viewer control to frmViewer.
- Select Microsoft's Common Dialog control from VB's component list and add the control to frmViewer.
- Set the common dialog control's name to cmndlg.
- Set the viewer control's properties as follows:
Name arv Height 7815 Left 120 Top 0 Width 13095 - Add the following code to the report:
Private Sub addButtonsToARV()
'Insert a splitter control
'in the second position (after the
'TOC button)
arv.ToolBar.Tools.Insert 1, ""
arv.ToolBar.Tools.Item(1).Type = 2
arv.ToolBar.Tools.Item(1).ID = 999
'Insert the Close botton in the
'third position
arv.ToolBar.Tools.Insert 2, "Close"
arv.ToolBar.Tools.Item(2).Caption = "&Close"
arv.ToolBar.Tools.Item(2).Tooltip = "Close Project"
arv.ToolBar.Tools.Item(2).ID = 1000
'Insert another splitter in the
'fourth position
arv.ToolBar.Tools.Insert 3, ""
arv.ToolBar.Tools.Item(3).Type = 2
arv.ToolBar.Tools.Item(3).ID = 1001
'Insert the Open button in the
'fifth position and assign it
'and icon
arv.ToolBar.Tools.Insert 4, "O&pen"
arv.ToolBar.Tools.Item(4).AddIcon LoadPicture("C:\Program Files\Microsoft _
Visual Studio\Common\Graphics\Icons\Win95\openfold.ico")
arv.ToolBar.Tools.Item(4).Tooltip = "Open RDF File"
arv.ToolBar.Tools.Item(4).ID = 1002
'Insert the Save button in the sixth
'position and assign it an icon and
'disable it
arv.ToolBar.Tools.Insert 5, "&Save"
arv.ToolBar.Tools.Item(5).AddIcon LoadPicture("C:\Program Files\Data _
Dynamics\ActiveReports Pro\Samples\Professional Edition\Diamond Reports_
\res\Standard\tfsave.ico")
arv.ToolBar.Tools.Item(5).Tooltip = "Save Report to RDF"
arv.ToolBar.Tools.Item(5).Enabled = False
arv.ToolBar.Tools.Item(5).ID = 1003
'Add the PDF export button to the
'end of the toolbar and disable it
arv.ToolBar.Tools.AddEx("&PDF").Tooltip = "Export To PDF"
arv.ToolBar.Tools.Item(arv.ToolBar.Tools.Count - 1).Enabled = False
arv.ToolBar.Tools.Item(arv.toolbar.tools.count-1).ID = 1004
End Sub - Add the following code to the Form_Load event:
Private Sub Form_Load()
addButtonsToARV
End Sub - Add the following code to the arv_ToolbarClick event:
Private Sub arv_ToolbarClick(ByVal Tool As DDActiveReportsViewer2Ctl.DDTool)
Select Case Tool.Caption
Case Is = "O&pen"
'Call the open sub
tbOpen
Case Is = "&Close"
'call the exit sub
tbExit
Case Is = "&PDF"
'call the PDFExport sub
tbPDFExport
Case Is = "&Save"
'call the Save sub
tbSave
End Select
End Sub - Add the following subs to handle the click events:
Private Sub tbExit()
Unload Me
End Sub
Private Sub tbOpen()
cmndlg.Filter = "Report Document File (*.rdf)|*.rdf"
cmndlg.ShowOpen
If cmndlg.FileName <> "" Then
If Not arv.ReportSource Is Nothing Then
Set arv.ReportSource = Nothing
End If
arv.Pages.Load cmndlg.FileName
'Enables buttons when a report is loaded
arv.ToolBar.Tools.Item(5).Enabled = True
arv.ToolBar.Tools.Item(arv.ToolBar.Tools.Count - 1).Enabled = True
End If
End Sub
Private Sub tbPDFExport()
Dim pdf As New ActiveReportsPDFExport.ARExportPDF
Set pdf = New ActiveReportsPDFExport.ARExportPDF
cmndlg.Filter = "Portable Document Format" & _
" (*.pdf)|*.pdf"
cmndlg.DefaultExt = ".pdf"
cmndlg.ShowSave
If cmndlg.FileName <> "" Then
pdf.FileName = cmndlg.FileName
If Not arv.ReportSource Is Nothing Then
pdf.Export arv.ReportSource.Pages
Else
pdf.Export arv.Pages
End If
End If
End Sub
Private Sub tbSave()
cmndlg.Filter = "Report Document File (*.rdf)|*.rdf"
cmndlg.DefaultExt = ".rdf"
cmndlg.ShowSave
If cmndlg.FileName <> "" Then
If Not arv.ReportSource Is Nothing Then
arv.ReportSource.Pages.Save _
cmndlg.FileName
Else
arv.Pages.Save cmndlg.FileName
End If
End If
End SubWarning: Setting the viewer's ReportSource = nothing while the report is still running does not cancel the report. If the ReportSource is set to nothing while the report is running, the viewer retains the pages already processed, and the table of contents does not work. To clear the viewer use .Pages.RemoveAll and then .Pages.Commit.
- Save the project and run it.