PrintDocument & Preview Work Don't Update RTF Box Changes Properly

larkahn

Member
Joined
Sep 10, 2006
Messages
18
Programming Experience
1-3
I have a button which, when clicked takes me to a page setup dialog box and after that to a print preview dialog box. It works fine the first time. If, though, I change any text in the rtf box, the preview still reflects the first preview plus part of the new change. I'm wondering if the problem is in the GetNextWord Function.

I am not a programmer and downloaded this code. Any assistance for a real novice would be appreciated.


VB.NET:
  Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles PrintDocument1.PrintPage
 
        Dim txtFont As New Font("IPAPhon", 14)
        Dim LMargin As Integer = PrintDocument1.DefaultPageSettings.Margins.Left
        Dim TMargin As Integer = PrintDocument1.DefaultPageSettings.Margins.Top
        Dim txtH As Integer = _
           PrintDocument1.DefaultPageSettings.PaperSize.Height - _
           PrintDocument1.DefaultPageSettings.Margins.Top - _
           PrintDocument1.DefaultPageSettings.Margins.Bottom
        Dim txtW As Integer = _
           PrintDocument1.DefaultPageSettings.PaperSize.Width - _
           PrintDocument1.DefaultPageSettings.Margins.Left - _
           PrintDocument1.DefaultPageSettings.Margins.Right
        Dim linesPerPage As Integer = _
           e.MarginBounds.Height / txtFont.GetHeight(e.Graphics)
        Dim R As New RectangleF(LMargin, TMargin, txtW, txtH)
        Static line As String
        Dim word As String
        Dim cols, lines As Integer
        word = GetNextWord()
        While word <> "" And lines < linesPerPage
            line = line & word
            word = GetNextWord()
            e.Graphics.MeasureString(line & word, txtFont, New SizeF(txtW, txtH), _
                                     New StringFormat(), cols, lines)
        End While
        If word = "" And Trim(line) <> "" Then
            e.Graphics.DrawString(line, txtFont, Brushes.Black, R, _
                                  New StringFormat())
            e.HasMorePages = False
            Exit Sub
        End If
        e.Graphics.DrawString(line, txtFont, Brushes.Black, R, New StringFormat())
        e.HasMorePages = True
        line = word
    End Sub
 
    Function GetNextWord(Optional ByVal reset As Boolean = False) As String
        Static currPos As Integer
        Dim word As String
        word = ""
 
        If reset Then currPos = 0
        If currPos >= RichTextBox1.Text.Length Then Return ""
        While Not System.Char.IsLetterOrDigit(RichTextBox1.Text.Chars(currPos))
            word = word & RichTextBox1.Text.Chars(currPos)
            currPos = currPos + 1
            If currPos >= RichTextBox1.Text.Length Then Return word
        End While
        While Not (System.Char.IsWhiteSpace(RichTextBox1.Text.Chars(currPos)))
            word = word & RichTextBox1.Text.Chars(currPos)
            currPos = currPos + 1
            If currPos >= RichTextBox1.Text.Length Then Return word
        End While
        Return word
    End Function
 
    Dim textToPrint As String
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        textToPrint = RichTextBox1.Text
 
        PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
        If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
        Else
            Exit Sub
        End If
 
        Try
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
    End Sub
 
 
End Class
 
Back
Top