Help with printing unable to advanced to next page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DeWittds
    New Member
    • Oct 2006
    • 11

    Help with printing unable to advanced to next page

    I have asked before and got little responce, So I try and try again.

    The code below prints the data in the same place but does not advance the page. I can see the lblnumber change but print in top of the previous one. So my loop is working, I have the hasmorepages= true set right after each cycle till the loop finishes.

    Hopefully someone can point out my error.

    Thanks David DeWitt

    this part calls the printing code
    Code:
     Public Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    
            Dim Print As New myPrinter
            Call Print.prt()
    
        End Sub
    here is my printing code
    Code:
    Imports System.Drawing.Printing
    
    Public Class myPrinter
        Public Shared lblnumber As Integer
        Public Shared endlbl As Integer
        Private WithEvents prn As PrintDocument
    
        Public Sub prt()
            Dim prn As New PrintDocument
    
            ' Create a PaperSize and specify the custom paper size .
            Dim pkCustomSize1 As New PaperSize("Label Size", 400, 800)
    
            Using (prn)
                prn.PrinterSettings.PrinterName = My.Settings.Printer
                'to add page size here.   
                prn.DefaultPageSettings.PaperSize = pkCustomSize1
    
                AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
                prn.Print()
                RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
            End Using
        End Sub
    
        Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As System.Drawing.Printing.PrintPageEventArgs)
            Dim smFont As New Font("Microsoft San Serif", 10)
            Dim medFont As New Font("Microsoft San Serif", 14)
            Dim lrgFont As New Font("Microsoft San Serif", 18)
            lblnumber = CInt(MainForm.txtStartBoxNUmber.Text)
            endlbl = CInt(MainForm.txtStartBoxNUmber.Text) + CInt(MainForm.txtBoxes.Text)
    
            Do
    
                ' return address
                args.Graphics.DrawImage(My.Resources.LOGO_2, 60, 10)
                args.Graphics.DrawString(My.Settings.returnAddress, New Font(smFont, FontStyle.Regular), Brushes.Black, 117, 70)
                args.Graphics.DrawString(My.Settings.returnCity, New Font(smFont, FontStyle.Regular), Brushes.Black, 110, 85)
                ' ship to address
                args.Graphics.DrawString("Ship to:", New Font(medFont, FontStyle.Bold), Brushes.Black, 5, 150)
                args.Graphics.DrawString(MainForm.txtCompanyName.Text, New Font(lrgFont, FontStyle.Bold), Brushes.Black, 30, 175)
                args.Graphics.DrawString(MainForm.txtAttn.Text, New Font(medFont, FontStyle.Regular), Brushes.Black, 30, 210)
                args.Graphics.DrawString(MainForm.txtAddress1.Text, New Font(medFont, FontStyle.Regular), Brushes.Black, 30, 235)
                args.Graphics.DrawString(MainForm.txtAddress2.Text, New Font(medFont, FontStyle.Regular), Brushes.Black, 30, 260)
                args.Graphics.DrawString(MainForm.txtCity.Text & ",  " & MainForm.txtState.Text & "  " & MainForm.txtZip.Text, New Font(medFont, FontStyle.Regular), Brushes.Black, 30, 285)
                ' Package & Order information
                args.Graphics.DrawString("P.O. #" & MainForm.txtPO.Text, New Font(smFont, FontStyle.Bold), Brushes.Black, 30, 370)
                args.Graphics.DrawString("Order #" & MainForm.txtOrderNumber.Text, New Font(smFont, FontStyle.Bold), Brushes.Black, 30, 390)
                args.Graphics.DrawString("Box #" & lblnumber, New Font(smFont, FontStyle.Bold), Brushes.Black, 30, 410)
    
                lblnumber = lblnumber + 1
                args.HasMorePages = True
            Loop Until lblnumber = endlbl
    
    
            args.HasMorePages = False
    
        End Sub
    End Class
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I believe that the loop is the problem:
    Code:
    Do Until lblnumber = endlbl
    {rest of code}
    Loop

    Comment

    • DeWittds
      New Member
      • Oct 2006
      • 11

      #3
      Try changing the do-loop as suggested. Still works the same. The only item that should change on each page is the lblnumber. It does changes but the page does not advance, so the information is printed on top of itself.
      In the days of quick basic I could send a form feed.

      Hope someone can figure out my error,
      David DeWitt

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        Have you tried to comment out the line:
        Code:
        args.HasMorePages = False
        My suggestion is to comment out lines until it stops working and then progress forward again. Sometimes it a case of one step backward and two forward. It must be frustrating to be so close :(.

        Comment

        • LX Apostal
          New Member
          • Apr 2007
          • 1

          #5
          This reply may be too late to help David DeWitt, but here is an answer that may help the next person...

          Help with printing unable to advanced to next page - using PrintDocument--------------------------------------------------------------------------------

          1) In David's code he has 1/2 the answer that everyone has been looking for. He calls RemoveHandler() to remove the handler when he was done drawing.
          AddHandler prn.PrintPage, AddressOf Me.PrintPageHan dler
          prn.Print()
          RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHan dler

          In the code I wrote that wasn't working. I was calling AddHandler more than once, which caused my code to draw more than one page on one sheet of paper.
          For example:
          Say I add 3 handlers, H1, H2, and H3.
          When I ask to PrintDocument1. Print(); the system calls H1 to handle a page, but the send to printer occurred when it calls H1 for the next page. My code sets the more pages flag "args.HasMorePa ges = True". But the system call H2 (not H1 again) which caused my code to move to drawing page two (2). My code sets the more pages flag "args.HasMorePa ges = True". Then the system call H3 (not H1 nor H2 again) which caused my code to move to drawing page three (3). Then my code says there are no more pages "args.HasMorePa ges = False". Then the system would call H1 again which causes the page to print, but now we are done, remember "args.HasMorePa ges = False"; so no more calls to Handlers, otherwise I would have seen a page ejection for each Handler.
          2) In David's code, the other half of the answer is you shouldn't set the "args.HasMorePa ges = True" flag in a loop that you are not exiting. David's code must exit the sub procedure PrintPageHandle r() after drawing the first (1) page. The system will call PrintPageHandle r() back again, and at that time he should draw page two (2). This means he has to save off the starting (or ending) record he has processed and start the second page on the record he left off on.

          For example:
          Private m_nLeftOffAt As Integer = 0
          Private m_bStartingOver As Boolean = True

          Private Sub PrintPageHandle r( ... )
          Dim nItemsOnThisPag e As Integer
          Dim nItemsWantedPer Page As Integer

          ' ... ' remove for readability

          lblnumber = CInt(MainForm.t xtStartBoxNUmbe r.Text)
          endlbl = CInt(MainForm.t xtStartBoxNUmbe r.Text) + CInt(MainForm.t xtBoxes.Text)

          If Not m_bStartingOver Then
          lblnumber = nLeftOffAt
          End If

          nItemsOnThisPag e = 0
          nItemsWantedPer Page = 60
          Do
          nItemsOnThisPag e = nItemsOnThisPag e + 1

          ' return address
          args.Graphics.D rawImage(My.Res ources.LOGO_2, 60, 10)
          ' ... ' remove for readability
          args.Graphics.D rawString("Box #" & lblnumber, New Font(smFont, FontStyle.Bold) , Brushes.Black, 30, 410)

          lblnumber = lblnumber + 1

          Loop Until (lblnumber = endlbl) Or (nItemsOnThisPa ge > nItemsWantedPer Page)

          If lblnumber >= endlbl Then
          args.HasMorePag es = False
          m_bStartingOver = True
          Else
          nLeftOffAt = lblnumber
          args.HasMorePag es = True
          End If
          End Sub


          Hope this helps someone.
          Thanks,
          Alex Apostal

          Comment

          Working...