dLSoft  barcode & labelling software     

Generating barcodes in Visual Basic 6

Barcodes may be created in Visual Basic 6 either as font barcodes, or as barcode images generated by calls to one of the dBarcode DLLs, or using the simplest approach by employing one of the Active Barcode Components as described here.

 

Active Barcode Component screen shot

Click to enlarge image

Adding the control to a Visual Basic form

To add an Active Barcode Component control to a Visual Basic form, select Components from the VB Project menu. If axbarcode Active-X Control module is displayed in the list of available controls, then ensure that it is checked. If it does not appear in the list user the Browse button to locate it in its installation directory (or in Windows System or System32), then check its name in the list. Once checked, an icon representing the component will appear in the toolbox.

Clicking on the Active Barcode Component icon in the toolbox causes the cursor to change to a cross. Positioning the cross on a Visual Basic form, holding down the left mouse button and dragging the cursor down and to the left, then releasing the mouse button, causes a rectangle to be drawn on the form. An image of a (default) barcode will be shown within this rectangle. You may wish to use this image of the component. Alternatively you may hide the component (by setting its Visible property to false) and use the components methods and properties to create an image that is placed elsewhere on the form - as in the sample Visual Basic program that is supplied with the component and uses code of the form:

Axbarcode1.ImageHeight = i
Axbarcode1.ImageWidth = j
Axbarcode1.Xunit = 0
Axbarcode1.CodeType = type
Axbarcode1.Caption = Text
Image1.Picture = Axbarcode1.Picture

A single Visual Basic Form may contain any number of Active Barcode Components. The first to be added will be called Axbarcode1, the second Axbarcode2, and so on; the names may be changed by the user by modifying the Name property within the Visual Basic Properties box.

Alternatively an array of controls may be created using the Index property.

Setting and retrieving property values

The Active Barcode Component may be operated entirely by setting or retrieving Property values using the Basic language.

Clicking on the Active Barcode Component control on the form when VB's Properties box is displayed will show the current settings for component's available properties. Most of these may be edited using the Properties box, or may have their values set from within the user's program by statements of the kind

            Axbarcode1.Source="12345"

Active Barcode Component properties that are set AFTER a Picture is created may be retrieved within user's programs by statements of the kind:

            x=Axbarcode1.ErrorCode

 Creating a barcode Picture within a program

Active Barcode Component produces its barcode image as a Windows metafile. This metafile is displayed directly in the area you create for the component - unless you set the Axbarcode.Visible property to FALSE.

To create a printable Picture object containing the barcode within a program the following steps may be used:

1. Set those properties that Active Barcode Component requires to generate the barcode, the minimum of which are

Axbarcode1.CodeType            - specifies the barcode type required
Axbarcode1.Caption    - specifies the characters which make up
   the code

2. Allow the component's image to be displayed on the form (if required).

OR

Copy the components Picture property to an image on the form, eg.

If Axbarcode1.ErrorCode = 0 Then
    Image1.Picture = Axbarcode1.Picture

3. Printing the Barcode on the Printer

To print the barcode image on the printer use the PaintPicture method of the Printer object, eg.

Printer.ScaleMode = 6  ' sets printer scale to mm
Printer.PaintPicture Axbarcode1.Picture, 20, 20, Axbarcode1.PictureWidth,     Axbarcode1.PictureHeight
Printer.NewPage
Printer.EndDoc

Saving barcode pictures

Barcode images may be saved in any of the supported graphics formats (BMP, EPS, GIF, PCX, PNG or WMF) by using the SaveImage() method:

er = Abc1.SaveImage(fname$)

where fname is the full path name (including extension the identifies the graphic type) of the  file that will receive the image, and er is -1 if the file is created successfully, or 0 otherwise.