ActiveReports allows you to use the hyperlink property to reference back to the bookmarks collection.
This walkthrough illustrates how to add hyperlinks to a report that references items in the bookmarks collection and create a directory to match the items in the bookmarks collection.
This walkthrough is split up into the following activities:- Adding two ActiveReports to a Visual Studio project
- Connecting the main report to a data source
- Adding controls to rptMain to display data
- Adding controls to rptBookmarks to display data
- Adding code to the main report's Detail_Format event
- Adding code to the main report's ReportEnd event
- Adding code to the second report's Detail_Format event
- Adding code to the second report's FetchData event
- Viewing the report
- Viewing the Bookmarks collection
To complete the walkthrough, you must have access to the Northwind database.
When you have completed this walkthrough, you will have a report that looks similar to the following.
Adding two ActiveReports to a Visual Studio project
To add two ActiveReports to a Visual Studio project
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptMain.
- Click Open.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptBookmarks.
- Click Open.
Connecting rptMain to a data source
To connect the report to a data source
- Click the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Select the "OLE DB" tab.
- Click the Build button.
- Select Microsoft Jet 4.0 OLE DB Provider and click Next.
- Click the ellipsis button to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select * from suppliers order by CompanyName".
- Click OK to return to the report design surface.
Adding controls to rptMain display the data
To add controls to the report
- Add a GroupHeader/Footer section to rptMain by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the Name property to ghSuppliers
- Change the DataField property to CompanyName
- Change the GroupKeepTogether property to All
- Change the KeepTogether property to True
- Add the following controls to ghSuppliers:
Control Name Text Location Label lblCompanyName Company Name 0, 0 Label lblContactName Contact Name 1.562, 0 Label lblPhone Phone # 2.687, 0 Label lblFax Fax # 3.812, 0 Label lblHomePage1 Home Page 4.875, 0 - In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section of rptMain and set the following properties of each textbox.
Field Text Location CompanyName Company Name 0, 0 ContactName Contact Name 1.562, 0 Phone Phone 2.687, 0 Fax Fax 3.812, 0 HomePage Home Page 4.875, 0 - Set the CanShrink property of the detail section to True.
- Add the following controls to the PageFooter section:
Control Name Text Miscellaneous Location Label lblEmail Need Assistance? E-mail Support: Support@company.com HyperLink = mailto:support@company.com 0, 0.125 Label lblHomePage Visit our home page: www.datadynamics.com HyperLink = www.datadynamics.com 0, 0.375
Adding controls to rptBookmarks display the data
To add controls to the report
- Set the CanShrink property of the detail section to True.
- Add the following controls to the PageHeader section:
Control Name Text Location Label lblDirectory Directory 0.5, 0 Label lblCompanyName Company Name 1.125, 0.437 Label lblPage Page 3.312, 0.437 Line Line1 X1 = 0
X2 = 6.5
Y1 = 0.697
Y2 = 0.697 - Add the following controls to the Detail section:
Control Name Text Location TextBox txtEntry Company Name 1.125, 0 TextBox txtPage Page 3.312, 0
Adding the code to rptMain's Detail_Format event
Add code to the Format event to parse the URL out of the HomePage field and assign it to the HyperLink property of txtHomePage, and to remove the URL markers from the text displayed in txtHomePage.
To write the code in Visual Basic or C#
- Double-click the Detail section of rptMain. This creates an event-handling method for rptMain's Detail_Format event.
The following example shows what the code for the method looks like.
' Visual Basic Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format Me.Detail1.AddBookmark(Me.txtCompanyName.Text) Dim iStart As Integer Dim sHTML As String If txtHomePage.Text <> "" Then iStart = InStr(1, txtHomePage.Text, "#", CompareMethod.Text) sHTML = Right(txtHomePage.Text, (Len(txtHomePage.Text) - iStart)) sHTML = Replace(sHTML, "#", "", 1, -1, CompareMethod.Text) txtHomePage.HyperLink = sHTML txtHomePage.Text = Replace(txtHomePage.Text, "#", "", 1, -1, CompareMethod _ .Text) End If End Sub //C# private void detail_Format(object sender, System.EventArgs eArgs) { this.detail.AddBookmark(this.txtCompanyName.Text); int iStart; string sHTML; if (txtHomePage.Text != "") { iStart = txtHomePage.Text.IndexOf("#",0); sHTML = txtHomePage.Text.Substring(iStart, txtHomePage.Text.Length - iStart); sHTML = sHTML.Replace("#", ""); txtHomePage.HyperLink = sHTML; txtHomePage.Text = txtHomePage.Text.Replace("#", ""); } }
Adding the code to rptMain's ReportEnd event
Add code to the ReportEnd event to run rptBookmarks and insert its pages collection at the beginning of rptMain.
To write the code in Visual Basic
- Right-click in any section of the design surface of rptMain, and click on View Code to display the code view for the report. At the top left of the code view for rptMain, click the drop-down arrow and select (rptMain Events). At the top right of the code window, click the drop-down arrow and select ReportEnd. This creates an event-handling method for the ReportEnd event.
To write the code in C#
- Click in the gray area below rptMain to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click ReportEnd. This creates an event-handling method for the ReportEnd event.
The following example shows what the code for the method looks like.
' Visual Basic Private Sub rptMain_ReportEnd(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles MyBase.ReportEnd Dim rpt As New rptBookmarks() rpt.pBM = Me.Document.Bookmarks rpt.Run() Me.Document.Pages.InsertRange(0, rpt.Document.Pages) End Sub //C# private void rptMain_ReportEnd(object sender, System.EventArgs eArgs) { rptBookmarks rpt = new rptBookmarks(); rpt.pBM = this.Document.Bookmarks; rpt.Run(); this.Document.Pages.InsertRange(0, rpt.Document.Pages); }
Adding the code to rptBookmarks' Detail_Format event
Add code to the Format event to create a hyperlink that points to a bookmark and to display the page number of the bookmark.
To write the code in Visual Basic or C#
- Double-click the Detail section of rptBookmarks. This creates an event-handling method for rptBookmarks' Detail_Format event.
The following example shows what the code for the method looks like.
'Visual Basic Public pBM As New BookmarksCollection() Dim iEntry As Integer Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format Me.txtEntry.HyperLink = "toc://" + pBM(iEntry - 1).Label Me.txtEntry.Text = pBM(iEntry - 1).Label Me.txtPage.Text = pBM(iEntry - 1).PageNumber End Sub //C# public BookmarksCollection pBM = new BookmarksCollection(); int iEntry; private void detail_Format(object sender, System.EventArgs eArgs) { this.txtEntry.HyperLink = "toc://" + pBM[iEntry - 1].Label; this.txtEntry.Text = pBM[iEntry - 1].Label; this.txtPage.Text = pBM[iEntry - 1].PageNumber.ToString(); }
Adding code to rptBookmarks' FetchData event
Add code to the FetchData event to get the page number of the bookmark.
To write the code in Visual Basic
- Right-click in any section of the design window of rptBookmarks, and click on View Code to display the code view for the report. At the top left of the code view for rptBookmarks, click the drop-down arrow and select (rptBookmarks Events). At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for rptBookmarks' FetchData event.
To write the code in C#
- Click in the gray area below rptBookmarks to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click FetchData. This creates an event-handling method for rptBookmarks' FetchData event.
The following example shows what the code for the method looks like.
' Visual Basic Private Sub rptBookmarks_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics _ .ActiveReports.ActiveReport.FetchEventArgs) Handles MyBase.FetchData If iEntry > pBM.Count - 1 Then eArgs.EOF = True Else eArgs.EOF = False iEntry += 1 End If End Sub
//C# private void rptBookmarks_FetchData(object sender, DataDynamics.ActiveReports .ActiveReport.FetchEventArgs eArgs) { if (iEntry > pBM.Count - 1) { eArgs.EOF = true; } else { eArgs.EOF = false; iEntry += 1; } }
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |
Viewing the Bookmarks Collection
To view the Bookmarks collection
- Press F5 to run the report.
- Click on the "Table of Contents" icon to view the Bookmarks collection.