Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 31st, 2004, 06:14 PM
Authorized User
 
Join Date: Jan 2004
Posts: 34
Thanks: 0
Thanked 1 Time in 1 Post
Default Printing

I am modifying the text editor example from [u]Beginning VB.NET 2nd Edition</u>. I copied the code for printing directly out of another chapter of the book. The problem is that it prints from a file not the textbox.

This is the code I use:

(strFileName is the file it is printing)

Code:
Public Sub Print()

            Dim objPrintDocument As PrintDocument = New PrintDocument

            objPrintDocument.DocumentName = "Print"

            PrintDialog1.AllowPrintToFile = False
            PrintDialog1.AllowSelection = True
            PrintDialog1.AllowSomePages = False

            PrintDialog1.Document = objPrintDocument

            If PrintDialog1.ShowDialog() = DialogResult.OK Then
                objStreamToPrint = New StreamReader(strFileName)

                objPrintFont = TextBox1.Font

                AddHandler objPrintDocument.PrintPage, AddressOf objPrintDocument_PrintPage

                objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings

                objPrintDocument.Print()

                objStreamToPrint.Close()
                objStreamToPrint = Nothing

            End If
    End Sub

    Private Sub objPrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        Dim sngLinesPerpage As Single = 0
        Dim sngVerticalPosition As Single = 0
        Dim intLineCount As Integer = 0
        Dim sngLeftMargin As Single = e.MarginBounds.Left
        Dim sngTopMargin As Single = e.MarginBounds.Top
        Dim strLine As String

        sngLinesPerpage = e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)

        strLine = objStreamToPrint.ReadLine()
        While (intLineCount < sngLinesPerpage And Not (strLine Is Nothing))
            sngVerticalPosition = sngTopMargin + (intLineCount * objPrintFont.GetHeight(e.Graphics))

            e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, sngLeftMargin, sngVerticalPosition, New StringFormat)

            intLineCount = intLineCount + 1

            If (intLineCount < sngLinesPerpage) Then
                strLine = objStreamToPrint.ReadLine()
            End If

        End While

        If (strLine <> Nothing) Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

    End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help about printing Fahim Crystal Reports 0 November 12th, 2006 11:14 PM
Printing Arunachalam Beginning VB 6 2 October 12th, 2006 07:44 AM
Printing uchkid Pro Visual Basic 2005 1 September 7th, 2006 03:37 AM
capturing printing settings when printing reports nikolaosk Access VBA 0 February 8th, 2005 04:14 AM
Printing NiNe ADO.NET 5 April 7th, 2004 04:28 AM





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