Solved

print preview from mdi active control - richtextbox

Posted on 2012-04-09
1
520 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
1 Comment
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37833078
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System32Int Error 8 57
Clone Visual studio 2013 solution 5 25
VB.NET bulk update  of a table from another table 4 36
fso.FolderExists("\\server\HiddenFolder$") 4 52
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now