We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

PrintPreviewDialog Printing Problem

Mr_Fulano
Mr_Fulano asked
on
Medium Priority
723 Views
Last Modified: 2012-08-13
Hi, I'm using VB.NET2010, WinForms. I have a PrintPreviewDialog on one of my Forms that prints the contents of a Textbox. It partially works, but not completely, because I can only print the last page of multiple page documents.

The PrintPreviewDialog doesn't give you much flexibility in terms of printer controls, and it's certainly not intuitive --- nothing in the printing spectrum is intuitive in Visual Studio, nor has it ever been.

Can someone please help me ensure that I can print the entire set of pages?

Thank you,
Fulano
#Region "Button PrintPreview"

    Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
        StringToPrint = tbxOutPut.Text
        Me.PrintPreviewDialog1.Document = Me.PrintDocument1
        Me.PrintPreviewDialog1.ShowDialog()
    End Sub

#End Region


#Region "PrintDocument 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 PrintFont As New Font("Courier", 7)

        PrintFont = tbxOutPut.Font

        Dim rectDraw As New RectangleF(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)

        If numChars < StringToPrint.Length Then

            StringToPrint = StringToPrint.Substring(numChars)

            e.HasMorePages = True

        Else

            e.HasMorePages = False

        End If
    End Sub

#End Region

Open in new window

Comment
Watch Question

Full Stack .NET Developer
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Carlos VillegasFull Stack .NET Developer
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Hi yv989c, you are correct. I can now print the complete number of pages. I will award you the 500 point. I'd also like you to look at my new question "How does Printing in VB.NET2010 work?" and try answering that for me, because although my problem was solved (much to you doing), I'm still a bit puzzled as to how printing happens in VB.NET2010.

Thanks,
Fulano

Author

Commented:
Thank you!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.