3 Replies - 450 Views - Last Post: 07 May 2013 - 11:44 AM Rate Topic: -----

#1 lucky.expert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-May 13

print_printpage continues to loop even though e.hasmorepages = false

Posted 07 May 2013 - 09:55 AM

So here is my situation: First of all, all of my printing code is stored in a module, which is called when I click the print button. My problem is, my pages print fine the first time I print my document, but if I click print again, the pages start printing on top of one another. When I debug, e.hasmorepages will be set to false, but it loops around and runs the print_page event again a couple times... I'm confused why it loops several times even though hasmorepages is definitely set to false?? The fact that usually prints right the first time but not the second time I click print makes the think something needs to be disposed. I know my page number variable is set to 1 before each print so it isn't that. The code I am using used to work until I moved it to it's own module.

Note: When I click print, i choose if I want a delivery recipt. if no, a store copy and customer copy are printed. If yes then Store, customer, and delivery copy are printed. Usually the Store and customer copy are printed on top of one another but the delivery copy is correct, so 2 pages are printed instead of 3.

Thanks for the help. I've been studying it for hours trying to find the problem with no luck.

Here is the basic outline of my module:

Imports System.Drawing
Imports System.Drawing.Printing

Module Receipt2
Public copy As Integer
Dim row As Integer

Dim ItemsRowCount As Integer = Invoice.dgvInvoiceItems.RowCount
Private Doc As New PrintDocument()

Public Sub printInvoice()

    Try
        copy = 1

        AddHandler Doc.PrintPage, AddressOf Print_PrintPage
        Doc.Print()
        row = 0
        Doc.Dispose()

        copy = 1

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)


    Try

        'RECEIPT ITEMS PRINTED HERE


        'print Store/customer copy and sig line
        If copy = 1 Then
            g.DrawString("Store Copy", New Font("Verdana", 15, FontStyle.Italic), Brushes.DarkRed, 50, 1045)


            e.HasMorePages = True
            copy = copy + 1
            row = 0
            Exit Sub
        ElseIf copy = 2 Then
            g.DrawString("Customer Copy", New Font("Verdana", 15, FontStyle.Italic), Brushes.DarkRed, 50, 1045)


            If Invoice.boolDeliveryReceipt = True Then
                e.HasMorePages = True
                copy = copy + 1
                row = 0
                Exit Sub
            else
                e.HasMorePages = false
            End If

        ElseIf copy = 3 Then
            g.DrawString("Delivery Copy", New Font("Verdana", 15, FontStyle.Italic), Brushes.DarkRed, 50, 1045)

            e.HasMorePages = false

        End If


    Catch ex As Exception

        MessageBox.Show(ex.Message)

    End Try
End Sub
End Module



Is This A Good Question/Topic? 0
  • +

Replies To: print_printpage continues to loop even though e.hasmorepages = false

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,572
  • Joined: 12-December 12

Re: print_printpage continues to loop even though e.hasmorepages = false

Posted 07 May 2013 - 10:20 AM

I suspect it is related to you setting copy = 1 twice in printInvoice(), or the fact that you AddHandler but don't remove it.

I'm surprised it runs the second time though, as you Dispose of Doc the first time round and don't instantiate it the second (etc.) time.

I'm not sure, but good luck.
Was This Post Helpful? 0
  • +
  • -

#3 lucky.expert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-May 13

Re: print_printpage continues to loop even though e.hasmorepages = false

Posted 07 May 2013 - 11:40 AM

Thanks for the input. turns out, i needed to create a new printdocument each time I printed. Once I adjusted the code to the following, it works like a charm:

Private Doc As PrintDocument

Public Sub printInvoice()

    Try
        copy = 1

        Doc = new PrintDocument()
        AddHandler Doc.PrintPage, AddressOf Print_PrintPage
        ItemsRowCount = Invoice.dgvInvoiceItems.RowCount
        Doc.Print()
        '' etc...


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,572
  • Joined: 12-December 12

Re: print_printpage continues to loop even though e.hasmorepages = false

Posted 07 May 2013 - 11:44 AM

View Postlucky.expert, on 07 May 2013 - 06:40 PM, said:

Thanks for the input. turns out, i needed to create a new printdocument each time I printed. Once I adjusted the code to the following, it works like a charm: ...

Yes, that looks more like it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1