Imports System.Drawing.Printing

Public Class Report

    Private WithEvents m_printDoc As New PrintDocument
    Private checkPrint As Integer

    Private Sub Report_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For Each entry In Binarypros.Report.Entries

            Dim start As Integer = RichTextBox1.TextLength
            RichTextBox1.AppendText(entry.Text)
            Dim [end] As Integer = RichTextBox1.TextLength

            RichTextBox1.Select(start, [end] - start)
            RichTextBox1.SelectionColor = entry.Color
            Dim font As New Font(RichTextBox1.SelectionFont.FontFamily, RichTextBox1.SelectionFont.Size, entry.FontStyle)
            RichTextBox1.SelectionFont = font
        Next
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim dlgSave As New SaveFileDialog()
        dlgSave.Filter = "Rich Text Format (RTF) | *.rtf"
        If dlgSave.ShowDialog() = Windows.Forms.DialogResult.OK Then
            RichTextBox1.SaveFile(dlgSave.FileName)
        End If
    End Sub

    Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
        Dim dlgPrint As New PrintDialog()
        dlgPrint.Document = m_printDoc
        If dlgPrint.ShowDialog() = Windows.Forms.DialogResult.OK Then
            m_printDoc.Print()
        End If

    End Sub

    Private Sub m_printDoc_BeginPrint(sender As Object, e As PrintEventArgs) Handles m_printDoc.BeginPrint
        checkPrint = 0
    End Sub

    Private Sub m_printDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles m_printDoc.PrintPage
        checkPrint = RichTextBox1.Print(checkPrint, RichTextBox1.TextLength, e)

        If checkPrint < RichTextBox1.TextLength Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

    Private Sub btnView_Click(sender As System.Object, e As System.EventArgs) Handles btnView.Click
        Binarypros.Report.Clear()
        Me.Close()
    End Sub
End Class