bpl1960 0 Newbie Poster

I'm having a hell of a time trying to print from an RTF box. I can do it, but for my test file (Lewis Carroll's "Fury said to a mouse..." with each word on a different line), I keep getting the first two pages printed on the same page, completely unintelligible, then the third page comes out okay. Here's all the relevant code--please forgive me for posting so much at once.

Const fiName As String = "C:\Fury.rtf"
Const v As String = ControlChars.CrLf

Dim DocHasHeader As Boolean = False
Dim HeaderHasPageNums As Boolean = False
Dim idx As Integer = -1

Dim msg As String = ""
Dim PageNum As Integer = 0
Dim TotalLines As Integer = 0



' PrintButton_Click prints the document, God willing.
Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
    idx = -1
    PageNum = 0
    PrintDoc.Print()
End Sub ' PrintButton_Click



' PrintDoc_BeginPrint sets the context for printing.
Sub PrintDoc_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs)
    If DocHasHeader Then
        If InStr(HeaderBox.Text, "#") > 0 Then
            PageNum = CInt(StartBox.Text) - 1
        End If
    End If

    TotalLines = DocRtf.Lines.LongCount
End Sub ' PrintDoc_BeginPrint



' PrintDoc_EndPrint completes printing.
Sub PrintDoc_EndPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs)
    MsgBox("Okay, we're done...")
End Sub ' PrintDoc_EndPrint



' PrintDoc_PrintPage prints 1 page at a time.
Sub PrintDoc_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDoc.PrintPage
    'Const Feed As String = ControlChars.FormFeed

    Dim myBrush As Brush = Brushes.Black
    Dim prFont As New Font("Courier New", 12, GraphicsUnit.Point)
    Dim LinesThisPage As Integer
    Dim Header As String = ""

    If TotalLines > 36 Then
        LinesThisPage = 36
    Else
        LinesThisPage = TotalLines
    End If

    PageNum += 1
    Header = " "
    If PrintHeaderCheck.Checked Then    ' A header exists.
        If Page1Check.Checked And PageNum > 1 Then
            Header = HeaderBox.Text
            Dim n As Integer = InStr(Header, "#")
            Mid(Header, n, 1) = PageNum.ToString
        End If
    End If

    With e.Graphics
        .DrawString(Header, prFont, myBrush, 100, 25)

        For lineCount = 0 To LinesThisPage - 1
            idx += 1
            Dim depth As Integer = 100 + 25 * lineCount
            .DrawString(DocRtf.Lines(idx), prFont, myBrush, 100, depth)
        Next

        '.DrawString(Feed, prFont, myBrush, 100, 10000)
    End With

    TotalLines -= LinesThisPage
    If TotalLines > 0 Then
        e.HasMorePages = True
    Else
        e.HasMorePages = False
        PageNum = 0
    End If
End Sub ' PrintDoc_PrintPage



' PWHForm_Load sets things up on program startup.
Private Sub PWHForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    DocRtf.LoadFile(fiName)

    With PrintDoc
        .DocumentName = fiName
        AddHandler .BeginPrint, AddressOf PrintDoc_BeginPrint
        AddHandler .PrintPage, AddressOf PrintDoc_PrintPage
        AddHandler .EndPrint, AddressOf PrintDoc_EndPrint
    End With
End Sub ' PWHForm_Load

`

What am I doing wrong? I've been spending weeks on this damn thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.