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 (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.
-
Click on File > 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
- Open a new project in Visual Studio.
- Click on Project > Add New Item.
- Select ActiveReports file and rename the file rptLabels.
- Click Open.
Connecting the data source to a database
To connect the data source to a database
- Click on the yellow report DataSource icon in the Detail section. This brings up the report DataSource dialog box.
- Click on Build...
- Select "Microsoft Jet 4.0 OLE DB Provider" and click Next >>.
- Click on the ellipsis to browse for the access path to Nwind.mdb. 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
- 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
- Set the PrintWidth of the report to the width of the label sheet less the Left and Right margins, or 8.1"
- Make the following changes to the Detail section:
- 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 in
- Set the height of the Detail section to the height of the label, 1"
- Add the following controls to the Detail section:
-
Control |
DataField |
Name |
Text/Caption |
Location |
TextBox |
ContactName |
txtContactName |
Contact Name |
0, 0 |
TextBox |
CompanyName |
txtCompanyName |
Company Name |
0, 0.198 |
TextBox |
Address |
txtAddress |
Address |
0, 0.396 |
TextBox |
City |
txtCity |
City |
0, 0.594 |
TextBox |
PostalCode |
txtPostalCode |
Zip Code |
0, 0.792 |
TextBox |
Country |
txtCountry |
Country |
1.5, 0.792 |
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 go to the Detail_Format event
- Add the following code to the event to repeat each label across all three columns.
' Visual Basic
Private Sub Detail_Format(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
Detail.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 a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports WinForm Viewer for help.
Tasks: Using the ActiveReports WinForm Viewer
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.