Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PrintPreviewDialog Printing Problem

Posted on 2011-04-23
4
Medium Priority
?
710 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

0
Comment
Question by:Mr_Fulano
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35454993
Hello, please add this lines to your form:
Private Sub PrintDocument1_BeginPrint(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        StringToPrint = tbxOutPut.Text
End Sub

Open in new window

0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 2000 total points
ID: 35454997
Then you can remove this bolded line from your code:

    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
0
 

Author Comment

by:Mr_Fulano
ID: 35457127
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
0
 

Author Closing Comment

by:Mr_Fulano
ID: 35457132
Thank you!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

564 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