Visual Studio
Barcodes in Visual Basic, C# & J# projects

How to add a barcode to a Visual Studio project

1

You can use the ActiveBarcode control in Visual Studio like you do with any other control, e.g. a button. First you have to add the ActiveBarcode control into the development environment.

1. Adding ActiveBarcode to the development environment

Go to the menu "Tools" and select the function "Choose Toolbox items":

Barcode, Visual Studio

2

A dialog appears. As ActiveBarcode is a COM component first select the tab "COM Components". A list of the available controls will be displayed. Select "ActiveBarcode" and activate it.

Barcode, Visual Studio

3

Then click OK. Now ActiveBarcode is added to your development environment.

2. Use ActiveBarcode in your project


4


Create a new project. Be aware that you select a project type that supports ActiveX controls:

Barcode, Visual Studio

5

To add the ActiveBarcode Control to a form you select the function "Toolbox" from the "View" menu first. This opens the toolbox. "Toolbox". Have a look or search for the ActiveBarcode Control in the toolbox now.

Barcode, Visual Studio

6

Click on ActiveBarcode there and move it the form you want to place the control. The control will then be placed on this form.

Barcode, Visual Studio

7

You can change the barcode properties of the barcode control now or later. In this example we change the background color to white:

Barcode, Visual Studio

8

Now add a "Textbox" to this form, too.

Barcode, Visual Studio

9

Double click this textbox now to open the source code for the event "Textchange". We will use this event to change the barcodes content every time the textbox changes.

Barcode, Visual Studio

10

This code example is for Visual Basic. With Visual C# you use the following code for the assignment og the text property:

axBarcode1.Text = textBox1.Text;
That's it. Now launch this example application:

Barcode, Visual Studio

11

If you change the content of the textbox in the running application, the barcode will encode this.

Optional: Non visual usage of ActiveBarcode

You also can create ActiveBarcode at run time and use the control:

In the following example ActiveBarcode is created invisible, then a Code 128 with the encoded content "Example" is generated and saved as image file by using the SaveAsBySize function.

Visual Basic:
  Dim MyObject As Object
  MyObject = CreateObject("ACTIVEBARCODE.BarcodeCtrl.1")
  MyObject.text = "Example"
  MyObject.typename = "Code 128"
  MyObject.SaveAsBySize("example.bmp", 400, 100)
  
Visual C#:
  BARCODELib.Barcode ab = new ACTIVEBARCODELib.Barcode();
  ab.Text = "123456789012";
  ab.TypeName = "EAN 13";
  ab.SaveAsBySize("example.bmp", 400, 150);
  

Optional: Printing a barcode with ActiveBarcode

It is assumed that the barcode control is visually stored on the form.

Visual Basic:
Transferring the barcode image to a PictureBox for example, for further use or processing:
PictureBox1.Image = AxBarcode1.Picture 
Print the barcode image by clicking on Button1. The barcode is printed in the upper left corner of the page:
Dim WithEvents PrintDocument1 As New Printing.PrintDocument()

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    e.Graphics.DrawImage(AxBarcode1.Picture, 0, 0)
End Sub

Visual C#:
Transferring the barcode image to a PictureBox for example, for further use or processing:
pictureBox1.Image = axBarcode1.Picture; 
Print the barcode image by clicking on Button1. The barcode is printed in the upper left corner of the page:
using System.Drawing.Printing;

private void button1_Click(object sender, EventArgs e)
{
  PrintDocument printDocument1 = new PrintDocument();
  printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
  printDocument1.Print();
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
  e.Graphics.DrawImage(axBarcode1.Picture, 0, 0);
}