?
Solved

VB.net - PrintDocument (NEW Page)

Posted on 2013-06-26
4
Medium Priority
?
2,558 Views
Last Modified: 2013-06-27
Hello Experts!
I found some code that works great for ONE Page, but I've now gotten to the point where I need two or more pages...and I can NOT figure out how to adjust this code for two or more pages...HELP!

Background...I have program that FTP files I keep track of all the events in a ListView...once the FTP is done, I want to print the lines in the ListView to a log for the operator to sign off on as completing the FTP.

I have class module that contains my PrintDocument and here is the code...how can I use this code to go to the second or thrid page if the linecount is >= to 600?

Namespace LogFiles
    Public Class LogFile
        ' Inherits all the functionality of a PrintDocument
        Inherits System.Drawing.Printing.PrintDocument

        ' Private variables to hold default font and text
        Private sClient As String
        Private sSite As String
        Private lvResults As ListView

        'Fonts
        Private Font1 As New Font("Arial", 14, FontStyle.Bold Or FontStyle.Underline)
        Private ClientNameFont As New Font("Arial", 14, FontStyle.Bold)
        Private Font2 As New Font("Arial", 10, FontStyle.Bold)
        Private Font3 As New Font("Arial", 10, FontStyle.Regular)

        ' Create pen.
        Private blackPen As New Pen(Color.Black, 1)

        Private RightJustified As New StringFormat
        Private LeftJustified As New StringFormat
        Private Centered As New StringFormat

        'Page Defaults:
        Private iDate As Int16 = 10
        Private iMessageType As Int16 = 126
        Private iMessageCode As Int16 = 216
        Private iMessage As Int16 = 300
        Private lTotalLines As Long = 600

        Public Sub New(ByVal FTPResults As ListView)
            MyBase.New()

            lvResults = FTPResults
        End Sub

        Public Property Client() As String
            Get
                Return sClient
            End Get
            Set(ByVal Value As String)
                sClient = Value
            End Set
        End Property

        Public Property ProcessingSite() As String
            Get
                Return sSite
            End Get
            Set(ByVal Value As String)
                sSite = Value
            End Set
        End Property

        Protected Overrides Sub OnBeginPrint(ByVal ev As System.Drawing.Printing.PrintEventArgs)
            ' Run base code
            MyBase.OnBeginPrint(ev)
        End Sub

        Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
            RightJustified.Alignment = System.Drawing.StringAlignment.Far
            LeftJustified.Alignment = System.Drawing.StringAlignment.Near
            Centered.Alignment = System.Drawing.StringAlignment.Center

            '--------------------------------- Title ------------------------------------------
            e.Graphics.DrawString("FTP File Results", Font1, Brushes.Black, 425, 50, Centered)
            e.Graphics.DrawString(sClient, ClientNameFont, Brushes.Black, 425, 70, Centered)
            '----------------------------------------------------------------------------------

            '------------------------- Detail Info ---------------------------------------
            ' Create Top Header Points that define line.
            Dim HeaderPoint11 As New Point(10, 180)
            Dim HeaderPoint12 As New Point(820, 180)
            Dim HeaderPoint21 As New Point(10, 185)
            Dim HeaderPoint22 As New Point(820, 185)

            ' Draw line:
            e.Graphics.DrawLine(blackPen, HeaderPoint11, HeaderPoint12)
            e.Graphics.DrawLine(blackPen, HeaderPoint21, HeaderPoint22)

            e.Graphics.DrawString("Date", Font3, Brushes.Black, 10, 195, LeftJustified)
            e.Graphics.DrawString("Message Type", Font3, Brushes.Black, 180, 195, LeftJustified)
            e.Graphics.DrawString("Message Code", Font3, Brushes.Black, 300, 195, LeftJustified)
            e.Graphics.DrawString("Message", Font3, Brushes.Black, 425, 195, LeftJustified)

            Dim HeaderPoint31 As New Point(10, 220)
            Dim HeaderPoint32 As New Point(820, 220)
            Dim HeaderPoint41 As New Point(10, 225)
            Dim HeaderPoint42 As New Point(820, 225)

            ' Draw line:
            e.Graphics.DrawLine(blackPen, HeaderPoint31, HeaderPoint32)
            e.Graphics.DrawLine(blackPen, HeaderPoint41, HeaderPoint42)

            Dim lLineNumber As Long = 245
            Try
                For iCount As Int32 = 0 To lvResults.Items.Count - 1
                    e.Graphics.DrawString(lvResults.Items.Item(iCount).Text, Font3, Brushes.Black, 10, lLineNumber, LeftJustified)
                    e.Graphics.DrawString(lvResults.Items.Item(iCount).SubItems(1).Text, Font3, Brushes.Black, 180, lLineNumber, LeftJustified)
                    e.Graphics.DrawString(lvResults.Items.Item(iCount).SubItems(2).Text, Font3, Brushes.Black, 300, lLineNumber, LeftJustified)
                    If lvResults.Items.Item(iCount).SubItems(3).Text.Length >= 55 Then
                        e.Graphics.DrawString(Strings.Left(lvResults.Items.Item(iCount).SubItems(3).Text, 55), Font3, Brushes.Black, 425, lLineNumber, LeftJustified)
                        lLineNumber = lLineNumber + 20
                        e.Graphics.DrawString(Strings.Mid(lvResults.Items.Item(iCount).SubItems(3).Text, 56, 55), Font3, Brushes.Black, 425, lLineNumber, LeftJustified)
                    Else
                        e.Graphics.DrawString(lvResults.Items.Item(iCount).SubItems(3).Text, Font3, Brushes.Black, 425, lLineNumber, LeftJustified)
                    End If
                    lLineNumber = lLineNumber + 20
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            e.Graphics.DrawString("Sent By:__________________________________________", Font3, Brushes.Black, 10, lLineNumber + 100, LeftJustified)
End Sub
End Class

Thanks,
Mark
0
Comment
Question by:smithmrk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39279501
You have to set e.HasMorePages to true and then adjust your printpage method to take current page into account

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printpageeventargs.hasmorepages.aspx
0
 

Author Comment

by:smithmrk
ID: 39281507
Thanks CodeCruiser, however I did play with e.HasMorePages, but when you say "Adjust my PrintPage Method"...that is where I'm stuck!

Can you help me with some sample code to the code I've already posted?

The problem I was having was I had the e.HasMorePages at the bottom of my Sub...but when the Sub fired again it reset everthing and basically start over again, and again, and again, looping over and over.

Thanks,
Mark
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39281857
Yeah trouble is that this is something you will get right by trial so I cant really change your code and make it fit for purpose. I think you will have to maintain a class level variable to keep record of how many lines your have printed (assuming that its the for loop printing details that makes it multipage) and then resume printing details section from where you left it

1) So a check on

lLineNumber = lLineNumber + 20

to see if you need to break on to next page. Move the e.HasMorePages in here and call it based on check on line number.

2) Change to loop

For iCount As Int32 = 0 To lvResults.Items.Count - 1

to start from the item that has not been printed yet
0
 

Author Closing Comment

by:smithmrk
ID: 39282103
OK, I figured it out.
Lightbulb went off when you suggested chaning my loop.
"Start from the item that has not been printed yet"

I appreciate the suggestions and comments!

Mark
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question