Walkthrough Hyperlinks and Bookmarks

ActiveReports allows you to use the hyperlink property to reference back to the bookmarks collection.

This walkthrough illustrates how to add to add hyperlinks to a report which reference 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 the reports 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

To complete the walkthrough, you must have access to the NorthWind database (Nwind.mdb). 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

1. Open a new project in Visual Studio.

2. Click on Project > Add New Item.

3. Select ActiveReports file and rename the file rptMain.

4. Click Open.

5. Click on Project > Add New Item.

6. Select ActiveReports file and rename the file rptBookmarks.

7. Click Open.

Connecting rptMain to a data source To connect the report to a data source

1. Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.

3. Select Microsoft Jet 4.0 OLE DB Provider and click Next >>

4. Click on the ellipsis to browse for the access path to NWind.mdb. Click Open once you have selected the appropriate access path.

5. Click OK to continue.

6. In the Query field, type "Select * from suppliers order by CompanyName".

7. Click OK to return to the report design surface.

Adding controls to display the data To add controls to the reports

1. Add a GroupHeader/Footer section to rptMain.

2. Make the following changes to the group header:

o Change the name to ghSuppliers o Change the DataField property to CompanyName o Change the GroupKeepTogether property to All o Change the KeepTogether property to True

3. Add the following controls to rptMain's GroupHeader section:

Control Name Text/Caption 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

lblHomePage!

Home Page

4.875, 0

4. Add the following controls to rptMain's Detail section:

Control DataField Name Text/Caption

TextBox CompanyName txtCompanyName Company Name

TextBox ContactName txtContactName Contact Name

TextBox Phone txtPhone Phone

TextBox Fax txtFax Fax

TextBox HomePage txtHomePage Home Page

Location

5. Add the following controls to rptMain's PageFooter section:

Control Name Text/Caption Misc Details Location

Label lblEmail mail Support: HyperLink = 0, 0.125

_ /v; mailto:[email protected]

[email protected] r '

Label lblHomePage Visit our home page: HyperLink = 0, 0.375

www.datadynamics.com www.datadynamics.com

Add the following controls to rptBookmarks' PageHeader section:

Control

Label Label Label

Line

Name

IblDirectory

IblCompanyName

IblPage

Linel

Text/Caption

Directory

CompanyName

Page

(Empty string)

Location

7. Add the following controls to rptBookmarks' Detail section:

Control Name Text/Caption

TextBox txtEntry Company Name 1.125, 0

TextBox txtPage Page 3.312, 0

Location

Adding the code to rptMain's Detail_Format event To write the code in Visual Basic

• Right-click in any section of the design window 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 Detail. At the top right of the code window, click the drop-down arrow and select Format. This creates an event-handling method for rptMain's Detail_Format event.

To write the code in C#

• Click on the Detail section of rptMain to select the section. Click on the events icon in the Properties window to display available events for the report. Double-click Format. 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 Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles Del Me.Detail.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.Tex End If

End Sub

private void Detail_Format(object sender, System.EventArgs eArgs) {

this.Detail.AddBookmark(this.txtCompanyName.Text); int iStart; string sHTML;

iStart = txtHomePage.Text.IndexOf("#",0);//[1, txtHomePage.Text, "#", C sHTML = txtHomePage.Text.Substring(iStart, txtHomePage.Text.Length - i; sHTML = sHTML.Replace("#", ""); txtHomePage.HyperLink = sHTML;

txtHomePage.Text = txtHomePage.Text.Replace("#", "");

Adding the code to rptMain's ReportEnd event 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 (Base Class 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 rptMain's 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 rptMain's ReportStart 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) Handle; Dim rpt As New rptBookmarks() rpt.pBM = Me.Document.Bookmarks rpt.Run()

Me.Document.Pages.InsertRange(Me.Document.Pages.Count, rpt.Document.Pages)

End Sub

private void rptMain_ReportEnd(object sender, System.EventArgs eArgs) {

rptBookmarks rpt = new rptBookmarks(); rpt.pBM = this.Document.Bookmarks; rpt.Run();

this.Document.Pages.InsertRange(this.Document.Pages.Count, rpt.Document.Pages);

Adding the code to rptBookmarks' Detail_Format event 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 Detail. At the top right of the code window, click the drop-down arrow and select Format. This creates an event-handling method for rptBookmarks' Detail_Format event.

To write the code in C#

• Click on the Detail section of rptBookmarks to select the section. Click on the events icon in the Properties window to display available events for the report. Double-click Format. 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 Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles Del Me.txtEntry.HyperLink = "toc://" + pBM(iEntry - 1).Label Me.txtEntry.Text = pBM(iEntry - 1).Label Me.txtPage.Text = pBM(iEntry - 1).PageNumber

End Sub

public BookmarksCollection pBM = new BookmarksCollection();

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 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 (Base Class 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.A ActiveReport.FetchEventArgs) Handles MyBase.FetchData If iEntry > pBM.Count - 1 Then eArgs.EOF = True

Else eArgs.EOF = False iEntry += 1 End If

int iEntry;

private void rptBookmarks_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport {

eArgs.EOF = true;

Viewing the report To view the report

1. Add the ActiveReports viewer control to a Windows Form.

2. Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports WinForm Viewer for help.

Was this article helpful?

0 0

Post a comment