ActiveReports can be used to print any label size by using the newspaper column layout.
This walkthrough illustrates how to create a report that prints labels to a laser printer labels sheet and to repeat labels using the LayoutAction property. The labels in this example are 1" x 2.5" and print 30 labels per 8½" x 11" sheet.
The walkthrough is split up into the following activities:
- Creating a new Visual Studio project
- Adding an ActiveReport to a Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Adding code to the detail_Format event to repeat labels
- Viewing the report
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.
When you have finished this walkthrough, you will have a report that looks similar to the following.
Creating a new Visual Studio project
To create a new Visual Studio project
- Open Visual Studio.
-
From the File menu, select New > Project.
- Select the project type and click on Windows Application.
- Change the name of your project and click OK.
Adding an ActiveReport to a Visual Studio project
To add an ActiveReport to your project
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptLabels.
- Click Open.
Connecting the data source to a database
To connect the data source to a database
- 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 ContactName, CompanyName, Address, City, PostalCode, Country FROM Customers".
- Click OK to return to the report design surface.
Adding controls to contain data
To add controls to the report
- Remove the PageHeader and Footer sections from the report by right-clicking in the PageHeader section and selecting "Delete."
- In the Report menu, click Settings and change the margins as follows:
- Top margin: 0.5
- Bottom margin: 0.5
- Left margin: 0.2
- Right margin: 0.2
- Select rptLabels in the Visual Studio Properties Window and Set the PrintWidth property of the report to 8.1 (the width of the label sheet less the Left and Right margins).
- Click on the detail section of the report to select it and make the following changes:
- Set the CanGrow and CanShrink properties to False
- Change the ColumnCount property to 3
- Change the ColumnDirection property to AcrossDown
- Set the ColumnSpacing property to 0.2
- Set the height of the Detail section to 1 (the height of the label)
- In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the Text and Location properties of each textbox as indicated.
Field Text Location ContactName Contact Name 0, 0 CompanyName Company Name 0, 0.198 Address Address 0, 0.396 City City 0, 0.594 PostalCode Postal Code 0, 0.792 Country Country 1.5, 0.792
Note: When you drag a field from the Report Explorer onto the design surface of the report, the DataField and Name properties of the textbox object are automatically set to the field.
Adding code to the detail_Format event to repeat labels
To add code to the the detail_Format event
- Double-click in the detail section to create a Detail_Format event
- Add the following code to the event to repeat each label across all three columns.
' Visual Basic Private Sub Detail1_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ Detail1.Format 'print each label three times Static counter As Integer counter = counter + 1 If counter <= 2 Then Me.LayoutAction = 3 Else Me.LayoutAction = 7 counter = 0 End If End Sub
//C# int counter=0; private void detail_Format(object sender, System.EventArgs eArgs) { //print each label three times counter = counter + 1; if (counter <= 2) { this.LayoutAction = LayoutAction.MoveLayout|LayoutAction.PrintSection; } else { this.LayoutAction = LayoutAction.MoveLayout|LayoutAction.NextRecord|LayoutAction .PrintSection; counter = 0; } }
Viewing the report
To view the report
- Add the ActiveReports viewer control to your Windows form and set its Dock property to Fill.
- 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.