none
How to use printpreview Dialog control?

    Question

  • I have used the openfiledialog to open, view and edit a text file in a rich text box. I would like to use the printpreview dialog box to preview the file and print it to the default printer. So far everything I have tried has failed. I admit that I don't know anything about using the printpreview dialog. Can someone tell me how I can get the file I have opened into the printpreview and print it? The code below is where I started and after many failed attempts this is where I have finally come back to.
     'Open File
        Private Sub Open_File(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
                rtx1.Text = sr.ReadToEnd
                sr.Close()
            End If
        End Sub
        ' Print Preview Dialog
        Private Sub Print_Preview_Document(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
            PrintPreviewDialog1.Document = Me.PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        End Sub
        ' Print Dialog
        Private Sub Print_Document(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintFile.Click
            PageSetupDialog1.Document = Me.PrintDocument1
            PageSetupDialog1.Document.DefaultPageSettings.Color = False
            PageSetupDialog1.ShowDialog()
        End Sub


    JR

    Tuesday, April 18, 2017 6:05 AM

Answers

  • I appreciate your response. I actually have tried that example verbatim except that example uses a hard coded file name and path. I want to use the path and name of the file I have opened with the openfiledialog box. this might be trivial but I cannot seem to do it.

    The path and filename that you have selected with the OpenFIleDialog is available as 'OpenFileDialog1.FileName'. You can break it into parts using Path.GetDirectoryName() for the directory portion and Path.GetfileName() for the filename portion.

    You need to use that instead of 'docPath' and 'docName' when setting the value for .DocumentName.  You could do that by passing it as an argument to ReadDocument. For instance

        Private Sub ReadDocument(docName As String, docPath As String) 
            printDocument1.DocumentName = docName
            Dim stream As New FileStream(docPath + docName, FileMode.Open)

    and then call that sub with the filename and path arguments.  However it appears that you have modified that example, so you may need to make some adjustments.

    But it doesn't seem relevant to what you are trying to do.  It appears that you already have the text loaded into the RTB, and that's what you want to print, not the file.   That is much more complex (the file contents and the RTB display are not the same). See, for example:
    https://support.microsoft.com/en-us/help/811401/how-to-print-the-content-of-a-richtextbox-control-by-using-visual-basic-.net-or-visual-basic-2005
    https://msdn.microsoft.com/en-us/library/7d29f66t(v=vs.90).aspx
    https://msdn.microsoft.com/en-us/library/ms996492.aspx

    • Marked as answer by J_Reid Wednesday, April 19, 2017 11:59 PM
    Tuesday, April 18, 2017 8:12 AM
  • Hi J_Reid,

    I put three Button, one RichTextBox, one PrintDocument, one PrintPreviewDialog control in Form, and do a simple sample, please refer to.

    Please note that set PrintPreviewDialog’s document=PrintDocument1

    Code Sample:

    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Open_Click(sender As Object, e As EventArgs) Handles Open.Click
            ' Create an OpenFileDialog object.
            Dim openFile1 As New OpenFileDialog()
    
            ' Initialize the filter to look for text files.
            openFile1.Filter = "Text Files|*.txt"
    
            ' If the user selected a file, load its contents into the RichTextBox. 
            If openFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                RichTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText)
            End If
    
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            PrintDocument1.Print()
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            If RichTextBox1.Text = "" Then
                MessageBox.Show("Please open or write sonething!")
            Else
                PrintPreviewDialog1.ShowDialog()
            End If
        End Sub
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim font As New Font("Arial", 16, FontStyle.Regular)
            e.Graphics.DrawString(RichTextBox1.Text, font, Brushes.Black, 100, 100)
        End Sub
    End Class

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, April 18, 2017 9:33 AM
    Moderator

All replies

  • There are samples in MSDN

    Like for example How to: Print in Windows Forms Using Print Preview

    Tuesday, April 18, 2017 6:09 AM
  • I appreciate your response. I actually have tried that example verbatim except that example uses a hard coded file name and path. I want to use the path and name of the file I have opened with the openfiledialog box. this might be trivial but I cannot seem to do it.

    JR

    Tuesday, April 18, 2017 6:25 AM
  • I appreciate your response. I actually have tried that example verbatim except that example uses a hard coded file name and path. I want to use the path and name of the file I have opened with the openfiledialog box. this might be trivial but I cannot seem to do it.

    The path and filename that you have selected with the OpenFIleDialog is available as 'OpenFileDialog1.FileName'. You can break it into parts using Path.GetDirectoryName() for the directory portion and Path.GetfileName() for the filename portion.

    You need to use that instead of 'docPath' and 'docName' when setting the value for .DocumentName.  You could do that by passing it as an argument to ReadDocument. For instance

        Private Sub ReadDocument(docName As String, docPath As String) 
            printDocument1.DocumentName = docName
            Dim stream As New FileStream(docPath + docName, FileMode.Open)

    and then call that sub with the filename and path arguments.  However it appears that you have modified that example, so you may need to make some adjustments.

    But it doesn't seem relevant to what you are trying to do.  It appears that you already have the text loaded into the RTB, and that's what you want to print, not the file.   That is much more complex (the file contents and the RTB display are not the same). See, for example:
    https://support.microsoft.com/en-us/help/811401/how-to-print-the-content-of-a-richtextbox-control-by-using-visual-basic-.net-or-visual-basic-2005
    https://msdn.microsoft.com/en-us/library/7d29f66t(v=vs.90).aspx
    https://msdn.microsoft.com/en-us/library/ms996492.aspx

    • Marked as answer by J_Reid Wednesday, April 19, 2017 11:59 PM
    Tuesday, April 18, 2017 8:12 AM
  • Hi J_Reid,

    I put three Button, one RichTextBox, one PrintDocument, one PrintPreviewDialog control in Form, and do a simple sample, please refer to.

    Please note that set PrintPreviewDialog’s document=PrintDocument1

    Code Sample:

    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Open_Click(sender As Object, e As EventArgs) Handles Open.Click
            ' Create an OpenFileDialog object.
            Dim openFile1 As New OpenFileDialog()
    
            ' Initialize the filter to look for text files.
            openFile1.Filter = "Text Files|*.txt"
    
            ' If the user selected a file, load its contents into the RichTextBox. 
            If openFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                RichTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText)
            End If
    
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            PrintDocument1.Print()
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            If RichTextBox1.Text = "" Then
                MessageBox.Show("Please open or write sonething!")
            Else
                PrintPreviewDialog1.ShowDialog()
            End If
        End Sub
        Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim font As New Font("Arial", 16, FontStyle.Regular)
            e.Graphics.DrawString(RichTextBox1.Text, font, Brushes.Black, 100, 100)
        End Sub
    End Class

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, April 18, 2017 9:33 AM
    Moderator
  • Thank you Acamar,

    I had a routine for printing but was having trouble with it the link you provided was very helpful. I had not seen that particular link/article.

    Thanks again.

    JR


    JR

    Thursday, April 20, 2017 12:02 AM
  • Cherry,

    I like your code. It is a very simple solution to my problem. I have tried several things that were close to this but none of them worked. After looking at your code I can see what I was doing wrong and how to fix it.

    I also like the animation in your response, a little beyond my level at the moment but very cool.

    Thanks,

    JR


    JR

    Thursday, April 20, 2017 12:07 AM