Results 1 to 5 of 5

Thread: Print Rich Textbox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Posts
    30

    Question Print Rich Textbox?

    Is there any easy way to print an multiline rich textbox to an printer?

  2. #2
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Ive just done something similar, and let me tell you

    there is no easy way

    Add a PrintDocument to your program, also add PrintPreviewDialog so you can view a preview without actually printing it ( saves alot of paper and ink ).

    On your print preview button, add this code

    VB Code:
    1. PrintPreviewDialog1.Document = PrintDocument1
    2.         PrintPreviewDialog1.ShowDialog()

    And that will call
    VB Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    which is where you do all your printing.

    The most basic thing you could do is

    VB Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.                     e.Graphics.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)

    But that wont take into account the text fitting on the page. You have to manully determine when your text reaches the end of the paper width and height, to start printing on a new page.

    And if your RichTextBox contains formmated text, you got some serious work involved.

    Printing is no easy task IMO (Ive spent the last 2 days non stop coding working out a way to print a richtextbox when the contents was just plain text ( all the characters in there was the same font ) and my print function is about 100 lines!
    Do a google search for printing in VB.NET, because printing is a big subject

  3. #3
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Heres a picture of my printing a rich text box

    The RichTextBox contained just normal text. I wrote a function to convert it into a layout so you could see the HEX values of each caracter.
    With that I had to determine how many characters would fit in the width of the page, if it was too many, I had to reformat the layout to make sure everything fitted.
    Once i worked that out I start printing the contents one line at a time, when I reached the bottom of the page, I had to goto a new page, reset any position variables I was using, and continue printing off where I left off.

    All that while taking into account, paper size, margin size, landscape or portrait and fotn size.

    My print function now is an extremly complicated function and was very hard to do. But after all my problems fitted everything on, I think the headaches have paid off

  4. #4
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    If your still interested, i just came across this which looks like an easy way of doing it

    http://msdn.microsoft.com/library/de...ichTextBox.asp

  5. #5
    Hyperactive Member Dasiths's Avatar
    Join Date
    Apr 2001
    Location
    Colombo, Sri Lanka
    Posts
    331

    in vb6 it would look like this ........

    Code:
    'print the document
        On Error Resume Next
        If ActiveForm Is Nothing Then Exit Sub
        
    
        With dlgCommonDialog
            .DialogTitle = "Print"
            .CancelError = True
            .Flags = cdlPDReturnDC + cdlPDNoPageNums
            If ActiveForm.rtfText.SelLength = 0 Then
                .Flags = .Flags + cdlPDAllPages
            Else
                .Flags = .Flags + cdlPDSelection
            End If
            .ShowPrinter
            
            Me.ActiveForm.Timer10.Enabled = True
                    
            If Err <> MSComDlg.cdlCancel Then
                ActiveForm.rtfText.SelPrint .hdc
            End If
        End With
    It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
    -Aristotle As quoted in Rapid Development, chapter 8, page 167.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width