flag german flag uk flag us Tip: Bookmark this page with Ctrl-D (Win) or Apple-D (Mac) MainProductsKnow How / FAQDownloadStoreContact



DISCONTINUED PRODUCT. PLEASE REFER TO OUR NEW .NET BARCODE LIBRARY AND CONTROL.

Manual - Forms Control Code 128 - VB .net Sample

Following is a walkthrough of a sample project that demonstrates the use of the Code 128 control with VB .net. The project is located in the VB Sample folder in the installation directory.

Note

In order for the samples to work independently of the actual installation location, a copy of the control has been put into the bin\Debug folder of the sample project. Accordingly, the control is referenced in the References tab of the sample project to be in this location. For new projects this reference should point to the actual installation location. (If you followed the setup steps as outlined in the manual, dragging the control from the toolbox to a form will automatically point the reference to the correct location.)

Setting the control's properties

To update the barcode, simply set the control's properties and repaint it. Here's a code snippet from the sample. Each time the user hits the Refresh button, the barcode is updated:

  Private Sub refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles refresh.Click
    ' Set control's properties from form's fields and call Invalidate() for a repaint

    Code1281.dataToEncode = dataToEncode.Text
    Code1281.moduleHeight = moduleHeight.Text
    Code1281.moduleWidth = moduleWidth.Text

    If humanReadable.Checked Then
      Code1281.humanReadable = True
    Else
      Code1281.humanReadable = False
    End If

    If gs1.Checked Then
      Code1281.gs1 = True
    Else
      Code1281.gs1 = False
    End If

    Code1281.Invalidate()
  End Sub


Saving the barcode

Saving a barcode to a file is simple, using the createCode function of the control. Here's the respective snippet from the sample. First, update the control's properties; then, all you have to provide is the required output resolution (in dpi):

  Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
    ' Save code to a file, simply pass desired output resolution
    ' to createCode().

    refresh_Click(Nothing, Nothing)
    Dim code As Bitmap = Code1281.createCode(300)
    Dim dlg As SaveFileDialog = New SaveFileDialog()
    dlg.Filter = "PNG Images (*.png)|*.png|TIFF Images (*.tif)|*.tif||"

    Dim result As DialogResult = dlg.ShowDialog()
    If (result = Windows.Forms.DialogResult.OK) Then
       code.Save(dlg.FileName)
    End If
  End Sub


Printing the barcode

Printing the code first involves creating a PrintDocument and adding a handler for the actual printing function (this is boilerplate code for VB .net printing, nothing special). In the printing function, use the createCode function (providing the output resolution of the printer) and paint the code where you want to have it:

  Private Sub print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles print.Click
    ' Create print dialog and attach document to it

    Dim pd As New PrintDocument
    AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
    Dim dlg As PrintDialog = New PrintDialog
    dlg.Document = pd

    Dim result As DialogResult = dlg.ShowDialog()
    If (result = Windows.Forms.DialogResult.OK) Then
       pd.Print()
    End If
  End Sub

  Private Sub pd_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    ' Print out barcode, simply pass printer resolution
    ' to createCode() and position the bitmap where you want it

    refresh_Click(Nothing, Nothing)
    Dim code As Bitmap = Code1281.createCode(e.Graphics.DpiX)
    e.Graphics.PageUnit = GraphicsUnit.Millimeter
    e.Graphics.DrawImage(code, New Point(20, 20))
    e.HasMorePages = False
    End Sub


Using the control as a DLL / LIbrary

While in most cases the control will be used on a form, you are not required to do so. Here's how to create a Code 128 barcode, using the control as a library:

  ...

  Dim c As WolfSoftware.Forms_Control_128.Code128 = new WolfSoftware.Forms_Control_128.Code128

  ... // setting of properties omitted

  Dim code As Bitmap = c.createCode(300)
  c.Save("c:\\test.png")

  ...


Note that you will have to add a reference to the control manually if you are not using the control on a form: Click the project name in the solution explorer, right-click for Properties, locate the References tab, click Add....

Unlocking the control (removing the DEMO text)

To unlock the control (and remove the DEMO text in the barcode), you'll need the license information you received after completing your purchase in the store.

Simply call the unlock function of the control and provide your license info. It's recommended to do this in the New routine of the form, right after the InitializeComponent call:

  Public Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Unlock and remove the DEMO text
    Code1281.unlock("LXXXXX-XXXXX", "XXXXX-XXXX-XXXXXXXX")

    ' other initialization stuff
    ...

  End Sub

The unlock function returns a boolean that indicates if the unlock procedure was successful.