Solved

print preview from mdi active control - richtextbox

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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 …

773 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