Solved

print preview from mdi active control - richtextbox

Posted on 2012-04-09
1
519 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

18 Experts available now in Live!

Get 1:1 Help Now