Search code examples
vb.net

Why won't the rows greater than 64 go on a new page? VB.NET


I am writing a program to go through EDI files to pull information. Right now I am trying to get it to grab the contents of the entire file and print it out but I only was 64 lines per page but for some reason after it reaches 64 it just goes back to the top of the first page and overlaps the values. I need it so each page only holds 64 lines and beyond that it will create a new page to continue to print the report.

' Reset linesAlreadyPrinted for each file linesAlreadyPrinted = 0 linesPrinted = 0

    If File.Exists(pageFile) Then
        Using reader As New StreamReader(pageFile)
            While True
                Dim line As String = reader.ReadLine()

                If line IsNot Nothing Then
                    If linesPrinted = 64 Then
                        linesPrinted = 0
                        e.HasMorePages = True
                    End If

                    ' Draw the line on a new line
                    e.Graphics.DrawString(line, dataFont, Brushes.Black, leftMargin, topMargin + linesPrinted * dataFont.Height)

                    ' Increment linesPrinted
                    linesPrinted += 1
                    linesAlreadyPrinted += 1

                    If line.StartsWith("IEA") Then
                        Debug.WriteLine("IEA" & vbCrLf & pageNum & vbCrLf & line)
                        linesPrinted = 0
                        e.HasMorePages = False
                        Exit While
                    End If
                Else
                    Debug.WriteLine("End of file reached")
                    e.HasMorePages = False
                    Exit While
                End If
            End While

        End Using

        ' Update progress bar value
        ProgressBar1.Value = CInt((currentIndex / EDILines.Count) * 100)
    Else
        e.Graphics.DrawString("File not found", dataFont, Brushes.Black, leftMargin, topMargin)

        ' Increment currentIndex to move to the next row
        currentIndex += 1

        ' Update progress bar value
        ProgressBar1.Value = CInt((currentIndex / EDILines.Count) * 100)

    End If

Solution

  • Assuming your code is inside the PrintPage event, you'll need to change where your reader is kept because you're basically never telling the program to print another page.
    The way that event works is that it will continue to fire again if the HasMorePages is true. So you'll want to print the first 64 lines, then set HasMorePages to true if there are more lines and exit the event. Then the event will be fired again and you can print the next 64 etc. When the file runs out set HasMorePages to false and the event won't be fired again.

    This is more like what you'll have to do.

    ***Edited to stop when IEA is found in the file and move on to the next file.

    private reader as StreamReader
    private printer as new PrintDocument
    
    public sub printFiles(fileNameList as string())
    for each pagefile as string in fileNameList
        openReader(pagefile)
        printer.Print()
    next
    
    public sub openReader(pagefile as string)
        reader = new streamreader(pagefile)
    end sub
    
    public sub do_printpage (obj as object, e as printeventargs)
        e.HasMorePages = true  'assume more pages are necessary when this method ends
        dim pagelines as integer = 0 'the number of lines printed for this page
        while pagelines < 64 'each page will have 64 lines
            pagelines += 1
            Dim line As String = reader.ReadLine()
            if line isnot null then 
               If line.StartsWith("IEA") Then
                   e.HasMorePages = False ' If we have reached a line with IEA then stop printing
                   Exit While
                End If
                e.Graphics.DrawString(line, dataFont, Brushes.Black, leftMargin, topMargin + linesPrinted * dataFont.Height)
            else 'End of the file stop printing
                e.HasMorePages = false
                exit while
            end if
        end while
    '****I messed up with the check for the line being nothing.  It should just exit the sub with HasMorePages set above.
    end sub
    

    There's probably some issues with the code but you can get the drift.