e.HasMorePages?

OMRebel

Active member
Joined
Sep 27, 2006
Messages
44
Programming Experience
Beginner
Can someone take a look at this code for me and tell me how to correct it? I found this example on a website for priting text out of a RichTextBox, however, at the very end, the webpage's example didn't have the condition setup right to check and see if there is another page. The webpage I got this example from is:
http://www.startvbdotnet.com/controls/printdialog1.aspx

The problem is just under where I have the comment:
'***************** Problem with example from website

Thanks.

VB.NET:
Imports System.Drawing.Printing
Imports System.Windows.Forms.Form


Public Class frmChargeListing

    Private Sub frmChargeListing_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' filling textbox with some text
        RichTextBox1.Text = "This is just a test"

    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            ' showDialog method makes the dialog box visible at run time
            PrintDocument1.Print()
        End If
    End Sub

    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
        Try
            PrintPreviewDialog1.ShowDialog()
        Catch es As Exception
            MessageBox.Show(es.Message)
        End Try
    End Sub

    Private Sub PageSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetupToolStripMenuItem.Click
        With PageSetupDialog1
            .PageSettings = PrintDocument1.DefaultPageSettings
        End With
        Try
            If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
            End If
        Catch es As Exception
            MessageBox.Show(es.Message)
        End Try
    End Sub

    Private Sub PPControlToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PPControlToolStripMenuItem.Click
        Try
            PrintPreviewControl1.Document = PrintDocument1
        Catch es As Exception
            MessageBox.Show(es.Message)
        End Try
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        ' PrintPage is the foundational printing event.  This event gets fired for every
        ' page that will be printed
        Static intCurrentChar As Int32
        ' declaring a static variable to hold the position of the last printed char
        Dim font As New Font("Verdana", 14)
        ' initiating the font to be used for printing
        Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
        With PrintDocument1.DefaultPageSettings
            ' initializing local variables that contain the bounds of the printing area rectangle
            PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            ' initializing local variables to hold margin values that will serve
            ' as the X and Y coordinates for the upper left corner of the printing
            ' area rectangle.
            marginLeft = .Margins.Left
            marginTop = .Margins.Top
            ' X and Y coordinate
        End With

        If PrintDocument1.DefaultPageSettings.Landscape Then
            Dim intTemp As Int32
            intTemp = PrintAreaHeight
            PrintAreaHeight = PrintAreaWidth
            PrintAreaWidth = intTemp
            ' if the user selects landscape mode, swap the printing area height and width
        End If

        Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
        ' calculating the total number of lines in the document based on the height of
        ' the printing area and the height of the font
        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
        ' initializing the rectangle structure that defines the printing area
        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
        ' instantiating the StringFormat class, which encapsulates text layout information
        Dim intLinesFilled, intCharsFitted As Int32
        e.Graphics.MeasureString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, _
            New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
        ' calling MeasureString to determine the number of characters that will fit in
        ' the printing area rectangle
        e.Graphics.DrawString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)
        ' print the text to the page
        intCurrentChar += intCharsFitted
        ' advancing the current char to the last char printed on this page

        '***************** Problem with example from website
        < RichTextBox1.Text.Length Then
            If e.HasMorePages = True Then
            Else
                'HasMorePages tells the printing module whether another PrintPage event should be fired
                e.HasMorePages = False
                intCurrentChar = 0
            End If
        End If
    End Sub
End Class
 
Nevermind. Figured it out. It should have been setup as:
VB.NET:
        If intCurrentChar < RichTextBox1.Text.Length Then
            e.HasMorePages = True
        Else
            'HasMorePages tells the printing module whether another PrintPage event should be fired
            e.HasMorePages = False
            intCurrentChar = 0
        End If

I've finally figured out how to get some semblance of a report going. Whoo-hoo!
 
Question. If i wanted to use this setup to print out the listing in a datagrid, or to populate the textbox with a query, how could I do that?
 
Back
Top