Solved

VB.net - PrintDocument (NEW Page)

Posted on 2013-06-26
4
2,059 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
  • 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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now