Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

print preview from mdi active control - richtextbox

Posted on 2012-04-09
1
Medium Priority
?
536 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 1500 total points
ID: 37833078
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

971 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