p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 (http://p2p.wrox.com/forumdisplay.php?f=226)
-   -   Errata for Page 235 (http://p2p.wrox.com/showthread.php?t=68353)

corncrake May 24th, 2008 11:30 AM

Errata for Page 235
The errata (posted on 01/06/2006) page 235, "Text Deletions", is clear that the "entire section along with the code had to be rewritten...due to changes in the software."

I have downloaded code from your site (hoping that it would be the rewritten version), and it does not appear to have been rewritten.

This error occurs: 'AddressOf' operand must be the name of a method (without parentheses).

Where is the rewritten section? Thanks.

Thearon May 27th, 2008 05:02 AM

You can download the code for Beginning Microsoft Visual Basic 2008 which has the updated code for printing. This code should work in Visual Basic 2005 with no problems.


corncrake May 31st, 2008 03:31 PM

Thank you. I am new at this and had to tinker around a bit, but did get it to work.

Please update the errata for page 234 to include that the rewritten code can be found in CHAPTER 8 of the code download for Beginning Visual Basic 2008. It's Chapter 7 in the 2005 book.

Thearon June 30th, 2008 04:49 AM

OK, here it is. This example assumes you have text in a textbox that you want to print.

Add a PrintDialog control to your form. Then add the following class level declarations:

    Private strPrintRecord As String
    Private WithEvents DialogsPrintDocument As PrintDocument

Now add this procedure:

    Private Sub DialogsPrintDocument_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
        Handles DialogsPrintDocument.PrintPage

        'Declare variables
        Dim intCharactersToPrint As Integer
        Dim intLinesPerPage As Integer
        Dim strPrintData As String
        Dim objStringFormat As New StringFormat
        Dim objPrintFont As New Font("Arial", 10)
        Dim objPageBoundaries As RectangleF
        Dim objPrintArea As SizeF

        'Get the page boundaries
        objPageBoundaries = New RectangleF(e.MarginBounds.Left, _
            e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)

        'Get the print area based on page margins and font used
        objPrintArea = New SizeF(e.MarginBounds.Width, _
            e.MarginBounds.Height - objPrintFont.GetHeight(e.Graphics))

        'Break in between words on a line
        objStringFormat.Trimming = StringTrimming.Word

        'Get the number of characters to print
        e.Graphics.MeasureString(strPrintRecord, objPrintFont, objPrintArea, _
            objStringFormat, intCharactersToPrint, intLinesPerPage)

        'Get the print data from the print record
        strPrintData = strPrintRecord.Substring(0, intCharactersToPrint)

        'Print the page
        e.Graphics.DrawString(strPrintData, objPrintFont, Brushes.Black, _
            objPageBoundaries, objStringFormat)

        'If more lines exist, print another page
        If intCharactersToPrint < strPrintRecord.Length Then
            'Remove printed text from print record
            strPrintRecord = strPrintRecord.Remove(0, intCharactersToPrint)
            e.HasMorePages = True
            e.HasMorePages = False
        End If
    End Sub

Finally double click on the Print button on your form and add this code:

    Private Sub btnPrint_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnPrint.Click

        'Instantiate a new instance of the PrintDocument
        DialogsPrintDocument = New PrintDocument

        'Set the PrintDialog properties
        With PrintDialog1
            .AllowCurrentPage = False
            .AllowPrintToFile = False
            .AllowSelection = False
            .AllowSomePages = False
            .Document = DialogsPrintDocument
            .PrinterSettings.DefaultPageSettings.Margins.Top = 25
            .PrinterSettings.DefaultPageSettings.Margins.Botto m = 25
            .PrinterSettings.DefaultPageSettings.Margins.Left = 25
            .PrinterSettings.DefaultPageSettings.Margins.Right = 25
        End With

        If PrintDialog1.ShowDialog = DialogResult.OK Then
            'Set the selected printer settings in the PrintDocument
            DialogsPrintDocument.PrinterSettings = _

            'Get the print data
            strPrintRecord = txtFile.Text

            'Invoke the Print method on the PrintDocument
        End If
    End Sub


Thearon June 30th, 2008 04:51 AM

As a side note to all, you can open any *.vb file in your favorite text editor to view the code. You can also open *.vb files from VB 2008 in the VB 2005 IDE to view the code. You just can't open projects from 2008 in 2005.


All times are GMT -4. The time now is 01:47 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.