none
Print Multiple Pages RRS feed

  • Question

  • HI


    In a previous topic
    The barcode is designed by
    DataGridViewcells
    I want a way if the print page finishes going to a new page
    No matter how many pages or elements are added.

    print item

    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

    my edit code to Print Multiple Pages

      Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            On Error Resume Next
            Dim i As Integer = 1
            Dim x As Integer = 20
            Dim y As Integer = 20
            For Each bc As Image In barcodes
                If y < (e.MarginBounds.Bottom - 40) Then
                    e.Graphics.DrawImage(bc, New Point(x, y))
                Else
                    e.Graphics.DrawImage(bc, New Point(x, y))
                    e.HasMorePages = True
                    Exit Sub
                End If
                y += 60
                i += 1
                If Not e.HasMorePages Then i += 0
            Next
        End Sub



    • Edited by ahmeddc Thursday, August 1, 2019 7:51 AM
    Thursday, August 1, 2019 7:48 AM

Answers

  • Hi

    Here is another example that may help. This example needs controls set in the Designer as per the imiage below.

    This example tries to best fit barcode 'boxes' of any size.

    ' BARCODES DGV PRINT AND PAGING ONE FONT
    
    ' Since I do not own a BarCode reader
    ' or a Printer, some of the details
    ' here may be incorrect.
    
    ' Example uses font Code 128  
    ' Can be downloaded from
    ' http://www.barcodelink.net/barcode-font.php
    ' Place into Windows\Fonts folder.
    
    ' Some attempt to size fonts to
    ' best fit, a bit rough and ready
    ' but works reasonably well for 
    ' box sizes tested.
    
    ' Many of the hard coded sizes and
    ' settings used here could be made
    ' options and stored in My.Settings
    
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable
    
      ' set size of BarCode Print out boxes
      Dim BCwidth As Integer = 80
      Dim BCheight As Integer = 30
    
      ' set the BARCODE FONT you want to use
      ' size is adjusted dynamically to fit
      Dim BCfont As Font = New Font("Code 128", 200, FontStyle.Regular, GraphicsUnit.Document)
    
      ' set whether to use Start Stop 
      ' BARCODE character
      Dim UseStartStop As Boolean = False
      ' set the Start Stop character to use
      Dim StartStopChar As String = "*"
    
      ' set the TEXT FONT you want to use
      ' size is adjusted dynamically to fit
      Dim txtFont As Font = New Font("Courier New", 66, FontStyle.Regular, GraphicsUnit.Point)
    
      Dim mLeft As Integer = My.Settings.mLeft
      Dim mRight As Integer = My.Settings.mRight
      Dim mTop As Integer = My.Settings.mTop
      Dim mBottom As Integer = My.Settings.mBottom
    
      Dim CurrentDataRow As Integer
      Dim Started As Boolean = False
    
      ' only used to set up dummy test data
      Dim rand As New Random
      Dim samples As Integer = 400
    
      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.mLeft = PrintDocument1.DefaultPageSettings.Margins.Left
        My.Settings.mRight = PrintDocument1.DefaultPageSettings.Margins.Right
        My.Settings.mTop = PrintDocument1.DefaultPageSettings.Margins.Top
        My.Settings.mBottom = PrintDocument1.DefaultPageSettings.Margins.Bottom
        My.Settings.Loc = Location
        My.Settings.Sz = Size
      End Sub
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Size = My.Settings.Sz
        Location = My.Settings.Loc
        MinimumSize = New Size(500, 300)
    
        CheckBox1.Checked = My.Settings.LandScape
    
        PageSetupDialog1.EnableMetric = True
        PageSetupDialog1.PageSettings.Margins = New Printing.Margins(mLeft, mRight, mTop, mBottom)
        PrintDocument1.DefaultPageSettings.Margins = New Printing.Margins(mLeft, mRight, mTop, mBottom)
    
        With dt
          .Columns.Add("ID", GetType(Integer))
          .Columns.Add("CheckPrint", GetType(Boolean))
          .Columns.Add("BarCode", GetType(String))
          .Columns.Add("Price", GetType(Double))
          .Columns.Add("Num", GetType(Integer))
          .Columns.Add("Code", GetType(String))
          .Columns.Add("Name", GetType(String))
        End With
    
        With DGV
          .DataSource = dt
          .Columns("BarCode").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("BarCode").DefaultCellStyle.Font = New Font(BCfont.FontFamily, 30, FontStyle.Regular)
          .Columns("BarCode").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
          .Columns("BarCode").DefaultCellStyle.Padding = New Padding(0, 0, 0, 3)
          .Columns("BarCode").DefaultCellStyle.BackColor = Color.White
          .Columns("BarCode").DefaultCellStyle.ForeColor = Color.Black
          .Columns("Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
          .Columns("Code").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("Num").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("Num").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
          .Columns("Price").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("Price").DefaultCellStyle.Format = "0.00"
          .Columns("Price").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
          .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        End With
    
        Dim fw As Single = txtFont.SizeInPoints
        While TextRenderer.MeasureText("*9 999999 99999 9*", txtFont).Width > BCwidth / 5 * 4 Or TextRenderer.MeasureText("*9 999999 99999 9*", txtFont).Height > CInt(BCheight / 3)
          fw -= 0.1F
          txtFont = New Font(txtFont.FontFamily, fw, FontStyle.Regular, GraphicsUnit.Document)
        End While
    
        Started = True
      End Sub
      Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        ' ----------------------
        ' some dummy test data
        ' just for this example.
        ' Could come from a DataBase
        ' or file whatever. No data
        ' saving in this example but
        ' can easily be included.
        ' ----------------------
    
        DGV.SuspendLayout()
        Cursor = Cursors.WaitCursor
        With dt
          Dim fruit As New List(Of String)({"Kiwi", "Orange", "BlueBerry", "Banana", "Papaya", "Durian", "Grape"})
          For i As Integer = 1 To samples
            Dim frut As String = fruit(rand.Next(0, fruit.Count))
            Dim code1 As String = GetCode(frut)
            Dim Code2 As String = code1
            If CheckBox2.Checked Then
              Code2 = Code2.Replace(" ", Nothing)
            End If
            .Rows.Add(i, IIf(rand.Next(1, 5) = 1, False, True), Code2, rand.NextDouble() * rand.Next(19, 99), rand.Next(99, 999), code1, frut)
          Next
        End With
        DGV.ResumeLayout()
        Cursor = DefaultCursor
      End Sub
    
      ' --------------------------------
      ' ONLY USED IN SETTING UP DUMMY DATA
      Function GetCode(f As String) As String
        Dim lead As String = rand.Next(1, 9).ToString
        Dim md As String = rand.Next(11111, 999999).ToString
        Dim ed As String = rand.Next(11111, 99999).ToString
        Dim tail As String = rand.Next(0, 9).ToString
        Return lead & " " & md & " " & ed & " " & tail
      End Function
      ' --------------------------------
    
    
      Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        PrintDocument1.DefaultPageSettings.Landscape = CheckBox1.Checked
    
        CurrentDataRow = -1
        '  CurtrentLines = 0
      End Sub
      Private Sub PrintDocument1_PrintPage_1(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        Dim x As Integer = 3 ' h gap between \ 2
        Dim y As Integer = 3 ' v gap between \ 2
        Dim rx As Integer = e.PageSettings.Margins.Left + x
        Dim ry As Integer = e.PageSettings.Margins.Top + y
        Dim w As Integer = BCwidth
        Dim h As Integer = BCheight
        Dim fmt As New StringFormat
        fmt.LineAlignment = StringAlignment.Center
    
        e.Graphics.Clear(Color.White)
        For Each r As DataGridViewRow In GetCheckedRows(DGV, DGV.Columns("CheckPrint").Index)
          If r.Index > CurrentDataRow Then
    
            With e.Graphics
              x = rx : y = ry
              .DrawRectangle(Pens.LightGray, New Rectangle(rx, ry, w, h))
    
              ' print NAME
              fmt.Alignment = StringAlignment.Near
              .DrawString(r.Cells("Name").Value.ToString, txtFont, New SolidBrush(Color.Black), New Rectangle(x + 2, y + 1, w \ 2 - 2, h \ 3), fmt)
    
              ' print PRICE
              fmt.Alignment = StringAlignment.Far
              .DrawString(CDbl(r.Cells("Price").Value.ToString).ToString("0.00") & " EGP", txtFont, New SolidBrush(Color.Black), New Rectangle(x + w \ 2, y + 1, w \ 2 - 2, h \ 3), fmt)
    
              ' try to best fit BC
              Dim bc As String = r.Cells("Code").Value.ToString
              If CheckBox2.Checked Then
                bc = bc.Replace(" ", Nothing)
              End If
              If UseStartStop Then
                bc = StartStopChar & bc & StartStopChar
              End If
              Dim fw As Single = 600
              While TextRenderer.MeasureText(bc, BCfont).Width > BCwidth Or TextRenderer.MeasureText(bc, BCfont).Height > BCheight \ 3 + 3
                fw -= 0.1F
                BCfont = New Font(BCfont.FontFamily, fw, FontStyle.Regular)
              End While
    
              'y = y + (h \ 3 * 2)
              'fmt.Alignment = StringAlignment.Center
              '.DrawString(bc, BCfont, New SolidBrush(Color.Black), New Rectangle(rx, y, w, h \ 3), fmt)
    
              ' Print BARCODE
              x = rx + (BCwidth - TextRenderer.MeasureText(bc, BCfont).Width) \ 2
              y = ry + h \ 3
              .DrawString(bc, BCfont, New SolidBrush(Color.Black), x, y, StringFormat.GenericTypographic)
    
              ' print CODE text
              y += TextRenderer.MeasureText(bc, BCfont).Height - 2
              fmt.Alignment = StringAlignment.Center
    
              .DrawString(r.Cells("Code").Value.ToString, txtFont, New SolidBrush(Color.Black), New Rectangle(rx, y, w, h \ 3), fmt)
    
              If rx + w + w > e.MarginBounds.Width Then
                rx = e.PageSettings.Margins.Left + 3
                ry += h + 3
                If e.MarginBounds.Top + ry + h > e.MarginBounds.Height Then
                  e.HasMorePages = True
                  CurrentDataRow = r.Index
                  Exit Sub
                End If
              Else
                rx += w + 3
              End If
            End With
          End If
        Next
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' print dialog
        With PrintDialog1
          .AllowSomePages = True
          .AllowCurrentPage = True
          .AllowSelection = True
          .AllowPrintToFile = True
          .ShowNetwork = True
        End With
        If PrintDialog1.ShowDialog() = DialogResult.OK Then
          PrintDocument1.Print()
        End If
      End Sub
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' preview
        PrintPreviewDialog1.ShowDialog()
      End Sub
      Private Sub Button3_Click(sender As Object, e As EventArgs)
        ' settings
        PrintDialog1.ShowDialog()
      End Sub
      Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button3.Click
        PrintDocument1.DefaultPageSettings.Landscape = CheckBox1.Checked
        PageSetupDialog1.ShowDialog()
        CheckBox1.Checked = PrintDocument1.DefaultPageSettings.Landscape
      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
          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 DGV_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellValidated
        If e.ColumnIndex = DGV.Columns("Code").Index Then
          DGV("BarCode", e.RowIndex).Value = DGV("Code", e.RowIndex).Value.ToString
        End If
      End Sub
      Sub UpDateGrid()
        Cursor = Cursors.WaitCursor
        If Started Then DGV.Columns("BarCode").DefaultCellStyle.Font = New Font(BCfont.FontFamily, 30, FontStyle.Regular)
    
        For Each r As DataGridViewRow In DGV.Rows
          If Not r.Index = DGV.NewRowIndex Then
            With r
              Dim bc As String = .Cells("Code").Value.ToString
              If CheckBox2.Checked Then
                bc = bc.Replace(" ", Nothing)
              End If
              bc = "*" & bc & "*"
              .Cells("BarCode").Value = bc
            End With
          End If
        Next
        Cursor = DefaultCursor
      End Sub
      Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If Not Started Then Exit Sub
        Dim cb As CheckBox = DirectCast(sender, CheckBox)
        Select Case cb.Name
          Case "CheckBox1"
            My.Settings.LandScape = CheckBox1.Checked
        End Select
      End Sub
      Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        UpDateGrid()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by ahmeddc Friday, August 2, 2019 7:34 PM
    Thursday, August 1, 2019 6:25 PM
  • Hi,

    You can set the PrintPage PrintPageEventArgs parameter value to determine if there is extra page printing,see the following link:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/e78b9ab1-9205-4b27-ac04-d13a5d37355d/printing-multiple-pages-in-vbnet?forum=vbgeneral

    Public Class Form1
        Dim m_CurrentPage As Integer
        Dim m_PageCount As Int32
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            m_CurrentPage = 0
        End Sub
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            '....
            m_CurrentPage += 1
            e.HasMorePages = (m_CurrentPage < m_PageCount)
        End Sub
    
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Friday, August 2, 2019 7:34 PM
    Thursday, August 1, 2019 8:52 AM

All replies

  • Hi,

    You can set the PrintPage PrintPageEventArgs parameter value to determine if there is extra page printing,see the following link:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/e78b9ab1-9205-4b27-ac04-d13a5d37355d/printing-multiple-pages-in-vbnet?forum=vbgeneral

    Public Class Form1
        Dim m_CurrentPage As Integer
        Dim m_PageCount As Int32
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            m_CurrentPage = 0
        End Sub
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            '....
            m_CurrentPage += 1
            e.HasMorePages = (m_CurrentPage < m_PageCount)
        End Sub
    
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Friday, August 2, 2019 7:34 PM
    Thursday, August 1, 2019 8:52 AM
  • Hi,

    You can set the PrintPage PrintPageEventArgs parameter value to determine if there is extra page printing,see the following link:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/e78b9ab1-9205-4b27-ac04-d13a5d37355d/printing-multiple-pages-in-vbnet?forum=vbgeneral

    Public Class Form1
        Dim m_CurrentPage As Integer
        Dim m_PageCount As Int32
        Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            m_CurrentPage = 0
        End Sub
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            '....
            m_CurrentPage += 1
            e.HasMorePages = (m_CurrentPage < m_PageCount)
        End Sub
    
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    my edit code

     Dim barcodes As New List(Of Image)
        Dim m_CurrentPage As Integer
        Dim m_PageCount As Int32
        Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
            m_CurrentPage = 0
        End Sub
        Private pageNo As Integer = 1
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            On Error Resume Next
            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))
                If (bc IsNot Nothing) Then
                    e.HasMorePages = True
                Else
                    e.HasMorePages = False
                End If
                y += 60
                m_CurrentPage += 1
                e.HasMorePages = (m_CurrentPage < m_PageCount)
            Next
        End Sub

    I still can not move to the new page


    Thursday, August 1, 2019 9:40 AM
  • Hi

    Here is another example that may help. This example needs controls set in the Designer as per the imiage below.

    This example tries to best fit barcode 'boxes' of any size.

    ' BARCODES DGV PRINT AND PAGING ONE FONT
    
    ' Since I do not own a BarCode reader
    ' or a Printer, some of the details
    ' here may be incorrect.
    
    ' Example uses font Code 128  
    ' Can be downloaded from
    ' http://www.barcodelink.net/barcode-font.php
    ' Place into Windows\Fonts folder.
    
    ' Some attempt to size fonts to
    ' best fit, a bit rough and ready
    ' but works reasonably well for 
    ' box sizes tested.
    
    ' Many of the hard coded sizes and
    ' settings used here could be made
    ' options and stored in My.Settings
    
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable
    
      ' set size of BarCode Print out boxes
      Dim BCwidth As Integer = 80
      Dim BCheight As Integer = 30
    
      ' set the BARCODE FONT you want to use
      ' size is adjusted dynamically to fit
      Dim BCfont As Font = New Font("Code 128", 200, FontStyle.Regular, GraphicsUnit.Document)
    
      ' set whether to use Start Stop 
      ' BARCODE character
      Dim UseStartStop As Boolean = False
      ' set the Start Stop character to use
      Dim StartStopChar As String = "*"
    
      ' set the TEXT FONT you want to use
      ' size is adjusted dynamically to fit
      Dim txtFont As Font = New Font("Courier New", 66, FontStyle.Regular, GraphicsUnit.Point)
    
      Dim mLeft As Integer = My.Settings.mLeft
      Dim mRight As Integer = My.Settings.mRight
      Dim mTop As Integer = My.Settings.mTop
      Dim mBottom As Integer = My.Settings.mBottom
    
      Dim CurrentDataRow As Integer
      Dim Started As Boolean = False
    
      ' only used to set up dummy test data
      Dim rand As New Random
      Dim samples As Integer = 400
    
      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.mLeft = PrintDocument1.DefaultPageSettings.Margins.Left
        My.Settings.mRight = PrintDocument1.DefaultPageSettings.Margins.Right
        My.Settings.mTop = PrintDocument1.DefaultPageSettings.Margins.Top
        My.Settings.mBottom = PrintDocument1.DefaultPageSettings.Margins.Bottom
        My.Settings.Loc = Location
        My.Settings.Sz = Size
      End Sub
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Size = My.Settings.Sz
        Location = My.Settings.Loc
        MinimumSize = New Size(500, 300)
    
        CheckBox1.Checked = My.Settings.LandScape
    
        PageSetupDialog1.EnableMetric = True
        PageSetupDialog1.PageSettings.Margins = New Printing.Margins(mLeft, mRight, mTop, mBottom)
        PrintDocument1.DefaultPageSettings.Margins = New Printing.Margins(mLeft, mRight, mTop, mBottom)
    
        With dt
          .Columns.Add("ID", GetType(Integer))
          .Columns.Add("CheckPrint", GetType(Boolean))
          .Columns.Add("BarCode", GetType(String))
          .Columns.Add("Price", GetType(Double))
          .Columns.Add("Num", GetType(Integer))
          .Columns.Add("Code", GetType(String))
          .Columns.Add("Name", GetType(String))
        End With
    
        With DGV
          .DataSource = dt
          .Columns("BarCode").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("BarCode").DefaultCellStyle.Font = New Font(BCfont.FontFamily, 30, FontStyle.Regular)
          .Columns("BarCode").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
          .Columns("BarCode").DefaultCellStyle.Padding = New Padding(0, 0, 0, 3)
          .Columns("BarCode").DefaultCellStyle.BackColor = Color.White
          .Columns("BarCode").DefaultCellStyle.ForeColor = Color.Black
          .Columns("Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
          .Columns("Code").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("Num").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("Num").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
          .Columns("Price").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
          .Columns("Price").DefaultCellStyle.Format = "0.00"
          .Columns("Price").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
          .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        End With
    
        Dim fw As Single = txtFont.SizeInPoints
        While TextRenderer.MeasureText("*9 999999 99999 9*", txtFont).Width > BCwidth / 5 * 4 Or TextRenderer.MeasureText("*9 999999 99999 9*", txtFont).Height > CInt(BCheight / 3)
          fw -= 0.1F
          txtFont = New Font(txtFont.FontFamily, fw, FontStyle.Regular, GraphicsUnit.Document)
        End While
    
        Started = True
      End Sub
      Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        ' ----------------------
        ' some dummy test data
        ' just for this example.
        ' Could come from a DataBase
        ' or file whatever. No data
        ' saving in this example but
        ' can easily be included.
        ' ----------------------
    
        DGV.SuspendLayout()
        Cursor = Cursors.WaitCursor
        With dt
          Dim fruit As New List(Of String)({"Kiwi", "Orange", "BlueBerry", "Banana", "Papaya", "Durian", "Grape"})
          For i As Integer = 1 To samples
            Dim frut As String = fruit(rand.Next(0, fruit.Count))
            Dim code1 As String = GetCode(frut)
            Dim Code2 As String = code1
            If CheckBox2.Checked Then
              Code2 = Code2.Replace(" ", Nothing)
            End If
            .Rows.Add(i, IIf(rand.Next(1, 5) = 1, False, True), Code2, rand.NextDouble() * rand.Next(19, 99), rand.Next(99, 999), code1, frut)
          Next
        End With
        DGV.ResumeLayout()
        Cursor = DefaultCursor
      End Sub
    
      ' --------------------------------
      ' ONLY USED IN SETTING UP DUMMY DATA
      Function GetCode(f As String) As String
        Dim lead As String = rand.Next(1, 9).ToString
        Dim md As String = rand.Next(11111, 999999).ToString
        Dim ed As String = rand.Next(11111, 99999).ToString
        Dim tail As String = rand.Next(0, 9).ToString
        Return lead & " " & md & " " & ed & " " & tail
      End Function
      ' --------------------------------
    
    
      Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        PrintDocument1.DefaultPageSettings.Landscape = CheckBox1.Checked
    
        CurrentDataRow = -1
        '  CurtrentLines = 0
      End Sub
      Private Sub PrintDocument1_PrintPage_1(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        Dim x As Integer = 3 ' h gap between \ 2
        Dim y As Integer = 3 ' v gap between \ 2
        Dim rx As Integer = e.PageSettings.Margins.Left + x
        Dim ry As Integer = e.PageSettings.Margins.Top + y
        Dim w As Integer = BCwidth
        Dim h As Integer = BCheight
        Dim fmt As New StringFormat
        fmt.LineAlignment = StringAlignment.Center
    
        e.Graphics.Clear(Color.White)
        For Each r As DataGridViewRow In GetCheckedRows(DGV, DGV.Columns("CheckPrint").Index)
          If r.Index > CurrentDataRow Then
    
            With e.Graphics
              x = rx : y = ry
              .DrawRectangle(Pens.LightGray, New Rectangle(rx, ry, w, h))
    
              ' print NAME
              fmt.Alignment = StringAlignment.Near
              .DrawString(r.Cells("Name").Value.ToString, txtFont, New SolidBrush(Color.Black), New Rectangle(x + 2, y + 1, w \ 2 - 2, h \ 3), fmt)
    
              ' print PRICE
              fmt.Alignment = StringAlignment.Far
              .DrawString(CDbl(r.Cells("Price").Value.ToString).ToString("0.00") & " EGP", txtFont, New SolidBrush(Color.Black), New Rectangle(x + w \ 2, y + 1, w \ 2 - 2, h \ 3), fmt)
    
              ' try to best fit BC
              Dim bc As String = r.Cells("Code").Value.ToString
              If CheckBox2.Checked Then
                bc = bc.Replace(" ", Nothing)
              End If
              If UseStartStop Then
                bc = StartStopChar & bc & StartStopChar
              End If
              Dim fw As Single = 600
              While TextRenderer.MeasureText(bc, BCfont).Width > BCwidth Or TextRenderer.MeasureText(bc, BCfont).Height > BCheight \ 3 + 3
                fw -= 0.1F
                BCfont = New Font(BCfont.FontFamily, fw, FontStyle.Regular)
              End While
    
              'y = y + (h \ 3 * 2)
              'fmt.Alignment = StringAlignment.Center
              '.DrawString(bc, BCfont, New SolidBrush(Color.Black), New Rectangle(rx, y, w, h \ 3), fmt)
    
              ' Print BARCODE
              x = rx + (BCwidth - TextRenderer.MeasureText(bc, BCfont).Width) \ 2
              y = ry + h \ 3
              .DrawString(bc, BCfont, New SolidBrush(Color.Black), x, y, StringFormat.GenericTypographic)
    
              ' print CODE text
              y += TextRenderer.MeasureText(bc, BCfont).Height - 2
              fmt.Alignment = StringAlignment.Center
    
              .DrawString(r.Cells("Code").Value.ToString, txtFont, New SolidBrush(Color.Black), New Rectangle(rx, y, w, h \ 3), fmt)
    
              If rx + w + w > e.MarginBounds.Width Then
                rx = e.PageSettings.Margins.Left + 3
                ry += h + 3
                If e.MarginBounds.Top + ry + h > e.MarginBounds.Height Then
                  e.HasMorePages = True
                  CurrentDataRow = r.Index
                  Exit Sub
                End If
              Else
                rx += w + 3
              End If
            End With
          End If
        Next
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' print dialog
        With PrintDialog1
          .AllowSomePages = True
          .AllowCurrentPage = True
          .AllowSelection = True
          .AllowPrintToFile = True
          .ShowNetwork = True
        End With
        If PrintDialog1.ShowDialog() = DialogResult.OK Then
          PrintDocument1.Print()
        End If
      End Sub
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' preview
        PrintPreviewDialog1.ShowDialog()
      End Sub
      Private Sub Button3_Click(sender As Object, e As EventArgs)
        ' settings
        PrintDialog1.ShowDialog()
      End Sub
      Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button3.Click
        PrintDocument1.DefaultPageSettings.Landscape = CheckBox1.Checked
        PageSetupDialog1.ShowDialog()
        CheckBox1.Checked = PrintDocument1.DefaultPageSettings.Landscape
      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
          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 DGV_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellValidated
        If e.ColumnIndex = DGV.Columns("Code").Index Then
          DGV("BarCode", e.RowIndex).Value = DGV("Code", e.RowIndex).Value.ToString
        End If
      End Sub
      Sub UpDateGrid()
        Cursor = Cursors.WaitCursor
        If Started Then DGV.Columns("BarCode").DefaultCellStyle.Font = New Font(BCfont.FontFamily, 30, FontStyle.Regular)
    
        For Each r As DataGridViewRow In DGV.Rows
          If Not r.Index = DGV.NewRowIndex Then
            With r
              Dim bc As String = .Cells("Code").Value.ToString
              If CheckBox2.Checked Then
                bc = bc.Replace(" ", Nothing)
              End If
              bc = "*" & bc & "*"
              .Cells("BarCode").Value = bc
            End With
          End If
        Next
        Cursor = DefaultCursor
      End Sub
      Private Sub CheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If Not Started Then Exit Sub
        Dim cb As CheckBox = DirectCast(sender, CheckBox)
        Select Case cb.Name
          Case "CheckBox1"
            My.Settings.LandScape = CheckBox1.Checked
        End Select
      End Sub
      Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        UpDateGrid()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by ahmeddc Friday, August 2, 2019 7:34 PM
    Thursday, August 1, 2019 6:25 PM