Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.net - PrintDocument (NEW Page)

Posted on 2013-06-26
4
Medium Priority
?
2,688 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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