ActiveReports 11
Address Labels
ActiveReports 11 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Layout > Address Labels

ActiveReports can be used to print any label size by using the newspaper column layout.

This walkthrough illustrates how to create a report that repeats labels using the LayoutAction property and prints labels to a laser printer. 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:

Note: This walkthrough uses the Northwind database. By default, in ActiveReports, the NWind.mdb file is located in the [User Documents folder]\GrapeCity Samples\ActiveReports 11\Data folder.

When you have finished this walkthrough, you get a report that looks similar to the following at design time and at run time.

Design-Time Layout

Run-Time Layout

To add an ActiveReport to the Visual Studio project

  1. Create a new Visual Studio project.
  2. From the Project menu, select Add New Item.
  3. In the Add New Item dialog that appears, select ActiveReports 11 Section Report (code-based) and in the Name field, rename the file as rptLabels.
  4. Click the Add button to open a new section report in the designer.

See Adding an ActiveReport to a Project for information on adding different report layouts.

To connect the report to a data source

  1. On the detail section band, click the Data Source Icon.
  2. In the Report Data Source dialog that appears, on the OLE DB tab, next to Connection String, click the Build button.
  3. In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button to move to the Connection tab.
  4. Click the ellipsis (...) button to browse to your database, for example the NWind.mdb sample database. Click Open once you have selected the appropriate database path.
  5. Click the Test Connection button to see if you have successfully connected to the database.
  6. Click OK to close the Data Link Properties window and return to the Report Data Source dialog. Notice that the Connection String field gets filled automatically.
  7. In the Query field on the OLE DB tab, enter the following SQL query.
    SQL Query
    Copy Code
    SELECT ContactName, CompanyName, Address, City, PostalCode, Country
    FROM Customers
  8. Click OK to save the data source and return to the report design surface.

To create a layout for the report

  1. Right-click the PageHeader section and select Delete to remove the PageHeader and Footer sections from the report.
  2. In the Report menu, select Settings and change the margins as follows:
    • Top margin: 0.5
    • Bottom margin: 0.5
    • Left margin: 0.2
    • Right margin: 0.2
  3. In the Report Explorer, select Report and in the Properties Window, set the PrintWidth property to 8.1 (the width of the label sheet less the Left and Right margins).
  4. Click the detail section of the report to select it and in the Properties window, set the properties as follows.
    Property Name Property Value
    CanGrow False
    ColumnCount 3
    ColumnDirection AcrossDown
    ColumnSpacing 0.2
    Height 1
  5. From the toolbox, drag six TextBox controls onto the detail section and set the properties of each textbox as follows.

    TextBox1

    Property Name Property Value
    DataField ContactName
    Location 0, 0 in
    Size 2.5, 0.2 in
    Font > Bold True

    TextBox2

    Property Name Property Value
    DataField CompanyName
    Location 0, 0.2 in
    Size 2.5, 0.2 in

    TextBox3

    Property Name Property Value
    DataField Address
    Location 0, 0.4 in
    Size 2.5, 0.2 in

    TextBox4

    Property Name Property Value
    DataField City
    Location 0, 0.6 in
    Size 2.5, 0.2 in

    TextBox5

    Property Name Property Value
    DataField PostalCode
    Location 0, 0.8 in
    Size 1.45, 0.2 in

    TextBox6

    Property Name Property Value
    DataField Country
    Location 1.5, 0.8 in
    Size 1, 0.2 in
  6. Select all of the textboxes, and in the Properties Window, set the CanGrow property to False. This prevents overlapping text, but may crop data if one of the fields contains more data than the control size allows.

If you preview the report at this point, one copy of each label appears on the page.

To add code to the detail_Format event to repeat labels

  1. Double-click in the detail section to create a detail_Format event.
  2. Add the following code to the event to repeat each label across all three columns.  

To write the code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Format event.
Copy Code
'print each label three times
Static counter As Integer
counter = counter + 1
If counter <= 2 Then
    Me.LayoutAction = GrapeCity.ActiveReports.LayoutAction.MoveLayout Or
GrapeCity.ActiveReports.LayoutAction.PrintSection Else Me.LayoutAction = GrapeCity.ActiveReports.LayoutAction.MoveLayout Or
GrapeCity.ActiveReports.LayoutAction.NextRecord Or
GrapeCity.ActiveReports.LayoutAction.PrintSection counter = 0 End If

To write the code in C#

C# code. Paste JUST ABOVE the Format event.
Copy Code
int counter=0; 
C# code. Paste INSIDE the Format event.
Copy Code
//print each label three times
counter = counter + 1; 
if (counter <= 2) 
{ 
    this.LayoutAction =  GrapeCity.ActiveReports.LayoutAction.MoveLayout|
GrapeCity.ActiveReports.LayoutAction.PrintSection; } else { this.LayoutAction = GrapeCity.ActiveReports.LayoutAction.MoveLayout|
GrapeCity.ActiveReports.LayoutAction.NextRecord|
GrapeCity.ActiveReports.LayoutAction.PrintSection; counter = 0; }

To view the report

OR