Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing / HasMorePages 1

Status
Not open for further replies.

UncleCake

Technical User
Feb 4, 2002
355
0
0
US
Hi, I am really struggling over HasdMorePages in the PrintDocument control. What I need to do is print invoices for our customers. In VB6 I would set the printer and loop through the records. I manually determined if I needed a page break because I knew how many line items could fit on a page. If it did, I called another sub, which added header info like address, column headings, and lines. In case it mattered, it is a Pervasive DB.

I am now trying to print with VB 2005. I can get it to print by using the printDoc_PrintPage sub, but I don't know where I should be looping through my records and how to use the HasMorePages.

I have searched on the Internet for about 4 hours now and have tried coutless tests without doing it correctly.

I have included a test loop just as an example to try to get multiple pages to work. The PrintWorkWrap is a module level sub that automatically wraps the text.
Code:
    Private Sub printDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printDoc.PrintPage
        Dim BCL As New BC.Library
        Dim currFont As Drawing.Font
        Dim i As Int16

        currFont = New Drawing.Font("Arial", 10, Drawing.FontStyle.Regular)
        For i = 0 To 3
            BCL.PrintWordWrap(e, currFont, 50, 50, 200, "1For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life.")
            BCL.PrintWordWrap(e, currFont, 75, 550, 200, "2For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life.")
            If i < 3 Then
                e.HasMorePages = True
                'Exit For
            Else
                e.HasMorePages = False
            End If
        Next i
      
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim POC As New BC.PrintOptionsClass
        Dim printDoc As New PrintDocument

        AddHandler printDoc.PrintPage, AddressOf printDoc_PrintPage

        POC.GetOptions(printDoc)
        printDoc.Print()
    End Sub
 
Hi UncleCake,

This is the last print routine I wrote, hope it helps ...


''' <summary>
''' Show preview of data before print
''' </summary>
''' <remarks></remarks>
Private Sub PreviewData()

' Prepare and show data
Me.Visible = False
PrintPreview.Document = PreparePrintDocument()
PrintPreview.WindowState = FormWindowState.Normal
PrintPreview.ShowDialog()
Me.Visible = True

End Sub

''' <summary>
''' Print out data
''' </summary>
''' <remarks></remarks>
Private Sub PrintData()

' Prepare data and print
Me.Visible = False
PrintDialog.Document = PreparePrintDocument()
If PrintDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then PrintDialog.Document.Print()
Me.Visible = True

End Sub

''' <summary>
''' Create print document
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Private Function PreparePrintDocument() As PrintDocument

Dim MyPrintDocument As New PrintDocument
MyHeaderDone = False

AddHandler MyPrintDocument.PrintPage, AddressOf Print_PrintPage

Return MyPrintDocument

End Function

''' <summary>
''' Print
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)

Dim MyTitleFont As New Font("Arial", 16, FontStyle.Bold)
Dim MyPeriodFont As New Font("Arial", 8, FontStyle.Regular)
Dim MyBodyFont As New Font("Arial", 10, FontStyle.Regular)
Dim MyBodyFontBold As New Font("Arial", 10, FontStyle.Bold)
Dim MyDOB As Date = CDate(GetDOB())
Dim MyCount As Integer = 50

If MyHeaderDone = False Then
With e.Graphics
.DrawString("Siento Period Data Report", MyTitleFont, MyBrushCalendarCurrent, 260, MyCount)
MyCount += 60
.DrawString(My.Resources.ENGDataReport, MyBodyFont, MyBrushCalendarCurrent, 30, MyCount)
MyCount += 114
.DrawString("Personal", MyBodyFontBold, MyBrushCalendarCurrent, 30, MyCount)
.DrawString("Analysis", MyBodyFontBold, MyBrushCalendarCurrent, 440, MyCount)
MyCount += 20
.DrawString("My date of birth: " & UKDate(MyDOB, DisplayStyles.FullDate), MyBodyFont, MyBrushCalendarCurrent, 30, MyCount)
.DrawString("Average period duration: " & MyMenses.AveragePeriodCycle.ToString("#0.0") & " days", MyBodyFont, MyBrushCalendarCurrent, 440, MyCount)
MyCount += 14
.DrawString("Number of periods in this report: " & Menses.PeriodsRecorded.ToString, MyBodyFont, MyBrushCalendarCurrent, 30, MyCount)
.DrawString("Average period dry duration: " & MyMenses.AverageDryDuration.ToString("#0.0") & " days", MyBodyFont, MyBrushCalendarCurrent, 440, MyCount)
MyCount += 14
.DrawString("Average period blood duration: " & MyMenses.AverageBleedDuration.ToString("#0.0") & " days", MyBodyFont, MyBrushCalendarCurrent, 440, MyCount)
MyCount += 14
.DrawString("Periods", MyBodyFontBold, MyBrushCalendarCurrent, 30, MyCount)
MyCount += 20
MyQueue = MyMenses.GetPeriodsReport
MyHeaderDone = True
End With
Else
e.Graphics.DrawString("Continued ...", MyTitleFont, MyBrushCalendarCurrent, 300, 50)
MyCount += 25
End If

Do Until MyQueue.Count < 1 Or MyCount >= 1100
Dim Bits() As String = Split(MyQueue.Dequeue.ToString & " | | ", "|")
e.Graphics.DrawString(Bits(0), MyPeriodFont, MyBrushCalendarCurrent, 30, MyCount)
e.Graphics.DrawString(Bits(1), MyPeriodFont, MyBrushCalendarCurrent, 370, MyCount)
e.Graphics.DrawString(Bits(2), MyPeriodFont, MyBrushCalendarCurrent, 690, MyCount)
MyCount += 14
Loop

' Tracking variables
e.HasMorePages = (MyQueue.Count > 0)

End Sub



DarkConsultant

Live long and prosper \\//
 
Hey Dark,

I went over your example and it looks similar to what I am doing with a header, but I print my header on each page (it looks like you just print it on the first page.)

What I still don't understand, which I am sure I am just looking over it, is how the loop is ran and how it works with the HasMorePges. I have read that the PrintPage sub sill be called over and over, but I don't understand how. Can you clarify that?

-UncleCake
 
Hi,

I assume that when the routine is called the HasMorePages keeps it being called WHAT! sorry I will try again. I believe that the HasMorePages variable forces function recalls until it is set to False. Thats sounds better.

Whenever I need a print routine I copy and paste the one above (which also contains a print preview) and modify the code inside the With e.Graphics to suit.

Good luck.

DarkConsultant

Live long and prosper \\//
 
NIce DarkConsultant. Now you just have to lear to use the ode tags to make it more eyepleasing. Click n the Process TGML link to learn more.



Christiaan Baes
Belgium

My Blog
 
Thanks DarkConsultant, I was able to print, now I have another question.

I am printing invoices from many different forms. Can I put the printing of the invoice in a module? I in VB6 I would pass it the invoice number and it would be printed.

Also, if I am printing in a form, do I have to declare the object that access my DB at at the form level (I am not sure what that is called)? I assume that I don't do it in the PrintPage sub .

-UncleCake
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top