ByteScout BarCode Generator SDK – VB.NET – Multiple Barcode Generation

  • Home
  • /
  • Articles
  • /
  • ByteScout BarCode Generator SDK – VB.NET – Multiple Barcode Generation

ByteScout BarCode Generator SDK – VB.NET – Multiple Barcode Generation


Imports System.IO
Imports System.Windows.Forms
Imports Bytescout.BarCode

Public Partial Class Form1
	Inherits Form
	Public Sub New()

        For Each st As SymbologyType In [Enum].GetValues(GetType(SymbologyType))

		cmbBarcodeType.SelectedIndex = 0
		cmbImageFormat.SelectedIndex = 0
	End Sub

    Private Sub btnGenerate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGenerate.Click
        Dim folderBrowserDialog As New FolderBrowserDialog()
        folderBrowserDialog.Description = "Select output folder"

        If folderBrowserDialog.ShowDialog() <> DialogResult.OK Then
        End If

        Dim fileNameTemplate As String = tbFileNameTemplate.Text & "." & Convert.ToString(cmbImageFormat.SelectedItem).ToLower()
        Dim counterStartAt As UInteger = CUInt(Math.Truncate(nudCounterStartAt.Value))
        Dim counterStepBy As UInteger = CUInt(Math.Truncate(nudCounterStepBy.Value))
        Dim counterDigits As UInteger = Convert.ToUInt32(cmbCounterDigits.Text)

        If Not fileNameTemplate.Contains("[N]") Then
            MessageBox.Show("File name template does not contain the counter placeholder [N]")
        End If

        Dim valueTemplate As String = tbValueTemplate.Text
        Dim valueStartAt As UInteger = CUInt(Math.Truncate(nudValueStartAt.Value))
        Dim valueStepBy As UInteger = CUInt(Math.Truncate(nudValueStepBy.Value))
        Dim valueEndAt As UInteger = CUInt(Math.Truncate(nudValueEndAt.Value))
        Dim valueDigits As UInteger = Convert.ToUInt32(cmbValueDigits.Text)

        If Not valueTemplate.Contains("[N]") Then
            MessageBox.Show("Barcode value template does not contain the counter placeholder [N]")
        End If

        Cursor = Cursors.WaitCursor
        progressBar.Visible = True
        btnGenerate.Text = "Stop"

            progressBar.Value = CInt(valueStartAt)
            progressBar.Minimum = CInt(valueStartAt)
            progressBar.Maximum = CInt(valueEndAt)
            progressBar.Step = CInt(valueStepBy)

            GenerateBarcodes(valueTemplate, valueStartAt, valueEndAt, valueStepBy, valueDigits, fileNameTemplate, _
             counterStartAt, counterStepBy, counterDigits, folderBrowserDialog.SelectedPath)
        Catch exception As Exception
            Cursor = Cursors.Default
            progressBar.Visible = False
            btnGenerate.Text = "Generate multiple barcodes"
        End Try
    End Sub

	Private Sub GenerateBarcodes(valueTemplate As String, valueStartAt As UInteger, valueEndAt As UInteger, valueStepBy As UInteger, valueDigits As UInteger, fileNameTemplate As String, _
		counterStartAt As UInteger, counterStepBy As UInteger, counterDigits As UInteger, outputFolder As String)
		Dim barcode As New Barcode()
		barcode.RegistrationKey = "demo"
		barcode.RegistrationName = "demo"

		barcode.Symbology = CType(cmbBarcodeType.SelectedItem, SymbologyType)
		' Set up additional barcode properties here.
		'barcode.AdditionalCaption = "...";

		Dim c As UInteger = counterStartAt

		Dim i As UInteger = valueStartAt
		While i <= valueEndAt
            Dim value As String = String.Format("{0:D" & valueDigits & "}", i)
            Dim counter As String = String.Format("{0:D" & counterDigits & "}", c)
			Dim fileName As String = fileNameTemplate.Replace("&#91;N&#93;", counter)


			barcode.Value = valueTemplate.Replace("&#91;N&#93;", value)

			barcode.SaveImage(Path.Combine(outputFolder, fileName))

			c += counterStepBy

			i += valueStepBy
		End While
	End Sub
End Class



Imports System.Collections.Generic
Imports System.Windows.Forms

NotInheritable Class Program
	Private Sub New()
	End Sub
	''' <summary>
	''' The main entry point for the application.
	''' </summary>
	<STAThread> _
	Friend Shared Sub Main()
		Application.Run(New Form1())
	End Sub
End Class

  Click here to get your Free Trial version of the SDK