Understanding some Print Code

Posted on 2006-05-02
Last Modified: 2010-04-23
Hello Everyone,
    First.. before I begin.. this is not homework...
    I am trying to learn Visual Baisc on my own...
    I was reading a book Visual Basic 2005 Step by Step
    I was going over the following code... and I was trying to determine what the following line is being used for:

       e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)

    Per the helps files from Visual Basic it tells me the following:

      Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

   However, I really dont understand how that line is being used within the code... I dont see the returned item being
   applied to any part of the code?

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim numChars As Integer
        Dim numLines As Integer
        Dim stringForPage As String
        Dim strFormat As New StringFormat

        Dim rectDraw As New Rectangle(e.MarginBounds.Left, e.MarginBounds.Top, _
            e.MarginBounds.Width, e.MarginBounds.Height)

        Dim sizeMeasure As New SizeF(e.MarginBounds.Width, _
            e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))

        strFormat.Trimming = StringTrimming.Word

        e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)

        stringForPage = StringToPrint.Substring(0, numChars)

        e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)

        If numChars < StringToPrint.Length Then
            StringToPrint = StringToPrint.Substring(numChars)
            e.HasMorePages = True
            e.HasMorePages = False
            StringToPrint = RichTextBox1.Text
        End If
    End Sub

Any assitance would be appricated

thank you
Question by:tony_813
    LVL 2

    Expert Comment


    its is used to figure out how many chars and lines can fit based on sizeMeasure
    LVL 2

    Expert Comment

    and its being used right here
    stringForPage = StringToPrint.Substring(0, numChars)
    LVL 9

    Author Comment

    Hello MasterThai,  thank you for your quick response... sorry to ask more about this...

       How is the result of:

        e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)

      Being used in:

        stringForPage = StringToPrint.Substring(0, numChars)

      When the result of: e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
      was never assigned to a variable to be used in: stringForPage = StringToPrint.Substring(0, numChars)??

    LVL 2

    Accepted Solution

    its basically a wrapper to that sets and handles the details of the string being printed...

    say you have a string that is 400 charcters long with no spaces... this will make the printed page look correct along with handling other things to like font details
    LVL 9

    Author Comment

    Hello MasterThai...
        Thank you for your information..

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now