ActiveX Control Integration with VB.NET

The IDAutomation ActiveX barcode controls allow the generation of Windows-Enhanced Metafile images and printing from Visual Studio.NET. IDAutomation recommends using .NET Forms Barcode Controls instead of ActiveX if possible.

  1. Purchase or download and install the ActiveX Control.
  2. Open a VB .NET Project and choose Project | Add Reference.
    Add a Reference to .NET
  3. Select the appropriate IDAutomation Barcode Component from the COM components tab of the reference list. Click Select. Click OK.
    Add a Component to .NET
  4. The following code examples use the IDAutomation.com Linear Barcode ActiveX control in the object named axBarCode1 to print to the default printer:
Private Sub cmdPrintToPrinter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
cmdPrintToPrinter.Click
    Dim pd As New PrintDocument()  
    'Add an Event Handler for printing
    AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
     'Fire off the print page event.
    pd.Print()
End Sub
'Event handler for the PrintDocument
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
    Dim linesPerPage As Single = 0
    Dim yPos As Single = 0
    Dim count As Integer = 0
    Dim leftMargin As Single = ev.MarginBounds.Left    
'Left Margin of Page
    Dim topMargin As Single = ev.MarginBounds.Top    
'Top Margin of Page.
    Dim line As String = Nothing                                           
'Data that we will write to the printer
 
    ' Calculate the number of lines per page based on the height of the font that was sent.
    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
 
    ' Print each line of the file.
    line = "IDAutomation.com, Inc. VB Dot Net Example"
 
    'Set the y co-ordinate of where we are going to print the Line
    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
 
    'Send the string to the printer
    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
 
    'Increase the line count
    count += 1
 
    'Set the y position of the next line
    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
 
    'Set the second string to be printed. We have to account for the actual printing position of the bar code when displaying yPos in this line. 'yPos set above is actually the location that this line
    'will be printed on.
    line = "Printing the Enhanced Metafile version of the barcode at X=" & leftMargin & ", Y=" & yPos + (topMargin + ((count + 1)
* printFont.GetHeight(ev.Graphics)))
     
    'Print the string
    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
    'Increase the line count
 
    count += 1
 
    'Increase the ypos
    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
    'Print the enhanced metafile image of the bar code
    ev.Graphics.DrawImage(AxBarCode1.GetEnhWMF, leftMargin, yPos)
End Sub

An image may also be saved to a local hard drive with the following code:

Private Sub cmdSaveToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveToFile.Click
    AxBarCode1.SaveBarCode("C:\SavedBarCode.wmf")
End Sub

Copying the image to the clipboard is only supported with IDAutomation's .NET Forms Controls.