Solved

print preview from mdi active control - richtextbox

Posted on 2012-04-09
1
525 Views
Last Modified: 2012-05-02
I am trying to print a preview in my app of a richtextbox control of a mdi child form. Here is my code. The print works OK, but the preview shows behind the print dialog and after you press cancel you can see the print preview, but only the first line shows with elipses on the end. Any quick help would be greatly appreciated.

Private Sub PrintToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem1.Click

        'allow the user to select a range for printing
        PrintDialog1.AllowSomePages = True
        PrintDialog1.Document = PrintDocument1
        'Display the PrintDialog and capture the result
        Dim result As DialogResult = PrintDialog1.ShowDialog()
        'if the result was OK then print the document
        If (result = Windows.Forms.DialogResult.OK) Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            'PrintDocument1.Print()
            'set the printPreviewDialog to your current dialog
            Try
                PrintPreviewDialog1.Document = PrintDocument1
                PrintPreviewDialog1.WindowState = FormWindowState.Maximized
                'show the print preview dialog
                PrintPreviewDialog1.ShowDialog()
            Catch ex As Exception
                MsgBox("THE PRINTING OPERATION FAILED" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

    Private Sub PageSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetupToolStripMenuItem.Click
        PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
        If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        ' Determine the active child form.
        Dim activeChild As Form = Me.ActiveMdiChild

        ' If there is an active child form, find the active control, which
        ' in this example should be a RichTextBox.
        If (Not activeChild Is Nothing) Then
            Dim theBox As RichTextBox = _
              TryCast(activeChild.ActiveControl, RichTextBox)


            'Using myPrintDialog As New PrintDialog

            'Set the print document's settings by using the PrintDialog
            PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                Dim myFileName As String = IO.Path.Combine(IO.Path.GetTempPath, "myfile.rtf")

                theBox.SaveFile(myFileName, RichTextBoxStreamType.RichText)

                Dim PSI As New ProcessStartInfo

                PSI.UseShellExecute = True
                PSI.CreateNoWindow = True
                PSI.WindowStyle = ProcessWindowStyle.Hidden
                PSI.Verb = "PrintTo"
                PSI.FileName = myFileName
                PSI.Arguments = """" & PrintDialog1.PrinterSettings.PrinterName & """"
                Process.Start(PSI)

                'PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings

            End If

            'End Using

            Static pageNum As Integer
            Dim prFont As New Font("Verdana", 6, GraphicsUnit.Point)
            e.Graphics.DrawString("Page " & pageNum + 1, prFont, Brushes.Black, 700, 1050)
            'e.Graphics.DrawRectangle(Pens.Blue, 0, 0, 300, 100)
            e.Graphics.DrawString(theBox.ToString(), prFont, Brushes.Black, 10, 10)

            ''logic to determine whether we're done printing
            'pageNum = pageNum + 1
            'If pageNum <= 5 Then
            '    e.HasMorePages = True
            'Else
            '    e.HasMorePages = False
            '    pageNum = 0
            'End If
        End If

    End Sub

    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click

        'set the printPreviewDialog to your current dialog
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.WindowState = FormWindowState.Maximized

        'show the print preview dialog
        PrintPreviewDialog1.ShowDialog()

    End Sub

End Class
0
Comment
Question by:ktjamms2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37833078
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
make control visible based off other control value 7 44
Set arraylist as datatable 2 29
Run software updates from the website 6 50
Datatable / Dates ? 4 30
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…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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