locked
Print barcode label for items RRS feed

  • Question

  • HI

    What a user wants from me
    Print a barcode label for all items at once
    The user wants when creating a new invoice
      Use the Print button to print a sequential barcode label for all items at once
    In order to paste the barcode on the product for sale

    Draw the barcode based on the barcode label specified in the check box row check
    And draw more of the
    barcode labelwhen examining more than one row or all rows with a check box

    With the possibility to see the code before printing with PrintPreviewDialogand direct print.


    • Edited by ahmeddc Friday, July 12, 2019 11:42 PM
    Friday, July 12, 2019 11:41 PM

Answers

  • Hi

    Must get some sleep so rather than wait for you to reply, here is the same code as before but with rudimentary printing added.

    Needs, in addition PrintDialog1, PrintPreviewDialog1 and PrintDocument1 added via the Designer.

    There is a Direct Print Button2, a Print Preview Button3 and a Print Setting Button4 added via the Designer.

    ' BARCODE GENERATOR
    ' Form1 with empty DataGridView1
    ' and Button1, reference to the
    ' GenCode128 DLL,
    ' PrintDocument1, PrintDialog1
    ' and PrintPreviewDialog1
    Option Strict On
    Option Explicit On
    Imports GenCode128
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("CheckPrint", GetType(Boolean))
    			.Columns.Add("BarCode", GetType(Image))
    			.Columns.Add("Price", GetType(Integer))
    			.Columns.Add("Num", GetType(Integer))
    			.Columns.Add("Code", GetType(String))
    			.Columns.Add("Name", GetType(String))
    
    			.Rows.Add(True, Nothing, 1, 2, "abc123", "Kiwi!")
    			.Rows.Add(False, Nothing, 2, 4, "999", "Orange")
    			.Rows.Add(True, Nothing, 4, 8, "ZXCV", "BlueBerry")
    			.Rows.Add(True, Nothing, 14, 11, "FGR-789HJU", "Banana")
    			.Rows.Add(True, Nothing, 24, 48, "ZULU", "Papaya")
    			.Rows.Add(True, Nothing, 1, 99, "SMELLY", "Durian ")
    			.Rows.Add(True, Nothing, 114, 78, "778899", "Grape")
    		End With
    		With DataGridView1
    			.DataSource = dt
    			.Columns("BarCode").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    			.Columns("Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    		End With
    	End Sub
    	Dim barcodes As New List(Of Image)
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		barcodes.Clear()
    		For Each r As DataGridViewRow In GetCheckedRows(DataGridView1, 0)
    			With r
    				Dim im As Image = Code128Rendering.MakeBarcodeImage(.Cells("Price").Value.ToString & " " & .Cells("Num").Value.ToString & " " & .Cells("Code").Value.ToString & " " & .Cells("Name").Value.ToString, 1, True)
    				.Cells("BarCode").Value = im
    				barcodes.Add(im)
    			End With
    		Next
    	End Sub
    	Function GetCheckedRows(dgv As DataGridView, col As Integer) As List(Of DataGridViewRow)
    		Dim checkedRows As New List(Of DataGridViewRow)
    		For inx As Integer = 0 To dgv.RowCount - 1
    			dgv(1, inx).Value = Nothing
    			Dim checked As DataGridViewCheckBoxCell = TryCast(dgv(col, inx), DataGridViewCheckBoxCell)
    			If CBool(checked.EditedFormattedValue) Then checkedRows.Add(dgv.Rows(inx))
    		Next
    		Return checkedRows
    	End Function
    	Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    		Dim x As Integer = 20
    		Dim y As Integer = 20
    		For Each bc As Image In barcodes
    			e.Graphics.DrawImage(bc, New Point(x, y))
    			y += 60
    		Next
    	End Sub
    	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    		' DIRECT PRINT
    		Try
    			If barcodes.Count > 0 Then
    				PrintDocument1.Print()
    			Else
    				MessageBox.Show("Nothingto Print")
    			End If
    		Catch ex As Exception
    			MessageBox.Show("An error occurred while printing",
    					ex.ToString())
    		End Try
    	End Sub
    	Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    		PrintPreviewDialog1.Show()
    	End Sub
    	Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    		PrintDialog1.ShowDialog()
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by ahmeddc Sunday, July 14, 2019 10:52 PM
    Saturday, July 13, 2019 1:33 AM

All replies

  • Hi

    This is a thread about Printing the Barcode(s).

    OK, you have the Barcodes for all the rows needed, but where are they? In a List(Of Image) perhaps?


    Regards Les, Livingston, Scotland

    Saturday, July 13, 2019 12:35 AM
  • Hi

    Must get some sleep so rather than wait for you to reply, here is the same code as before but with rudimentary printing added.

    Needs, in addition PrintDialog1, PrintPreviewDialog1 and PrintDocument1 added via the Designer.

    There is a Direct Print Button2, a Print Preview Button3 and a Print Setting Button4 added via the Designer.

    ' BARCODE GENERATOR
    ' Form1 with empty DataGridView1
    ' and Button1, reference to the
    ' GenCode128 DLL,
    ' PrintDocument1, PrintDialog1
    ' and PrintPreviewDialog1
    Option Strict On
    Option Explicit On
    Imports GenCode128
    Public Class Form1
    	Dim dt As New DataTable("Freddy")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With dt
    			.Columns.Add("CheckPrint", GetType(Boolean))
    			.Columns.Add("BarCode", GetType(Image))
    			.Columns.Add("Price", GetType(Integer))
    			.Columns.Add("Num", GetType(Integer))
    			.Columns.Add("Code", GetType(String))
    			.Columns.Add("Name", GetType(String))
    
    			.Rows.Add(True, Nothing, 1, 2, "abc123", "Kiwi!")
    			.Rows.Add(False, Nothing, 2, 4, "999", "Orange")
    			.Rows.Add(True, Nothing, 4, 8, "ZXCV", "BlueBerry")
    			.Rows.Add(True, Nothing, 14, 11, "FGR-789HJU", "Banana")
    			.Rows.Add(True, Nothing, 24, 48, "ZULU", "Papaya")
    			.Rows.Add(True, Nothing, 1, 99, "SMELLY", "Durian ")
    			.Rows.Add(True, Nothing, 114, 78, "778899", "Grape")
    		End With
    		With DataGridView1
    			.DataSource = dt
    			.Columns("BarCode").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
    			.Columns("Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    		End With
    	End Sub
    	Dim barcodes As New List(Of Image)
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		barcodes.Clear()
    		For Each r As DataGridViewRow In GetCheckedRows(DataGridView1, 0)
    			With r
    				Dim im As Image = Code128Rendering.MakeBarcodeImage(.Cells("Price").Value.ToString & " " & .Cells("Num").Value.ToString & " " & .Cells("Code").Value.ToString & " " & .Cells("Name").Value.ToString, 1, True)
    				.Cells("BarCode").Value = im
    				barcodes.Add(im)
    			End With
    		Next
    	End Sub
    	Function GetCheckedRows(dgv As DataGridView, col As Integer) As List(Of DataGridViewRow)
    		Dim checkedRows As New List(Of DataGridViewRow)
    		For inx As Integer = 0 To dgv.RowCount - 1
    			dgv(1, inx).Value = Nothing
    			Dim checked As DataGridViewCheckBoxCell = TryCast(dgv(col, inx), DataGridViewCheckBoxCell)
    			If CBool(checked.EditedFormattedValue) Then checkedRows.Add(dgv.Rows(inx))
    		Next
    		Return checkedRows
    	End Function
    	Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    		Dim x As Integer = 20
    		Dim y As Integer = 20
    		For Each bc As Image In barcodes
    			e.Graphics.DrawImage(bc, New Point(x, y))
    			y += 60
    		Next
    	End Sub
    	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    		' DIRECT PRINT
    		Try
    			If barcodes.Count > 0 Then
    				PrintDocument1.Print()
    			Else
    				MessageBox.Show("Nothingto Print")
    			End If
    		Catch ex As Exception
    			MessageBox.Show("An error occurred while printing",
    					ex.ToString())
    		End Try
    	End Sub
    	Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    		PrintPreviewDialog1.Show()
    	End Sub
    	Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    		PrintDialog1.ShowDialog()
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by ahmeddc Sunday, July 14, 2019 10:52 PM
    Saturday, July 13, 2019 1:33 AM