CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2014
    Posts
    13

    How to print selected text of richtextbox?

    Here is my code to print a richtextbox file.

    But I have two problems:

    1. It doesn't print any text. Just blank paper comes out of my printer.

    2. I'd like to be able to optionally select text to print as an option. I've enabled that in my printDialog component.

    Thanks for any help.

    Code:
                PrintDocument printDoc = new PrintDocument();
                printDoc.DocumentName = mFullPathFileName;
                printDialog.Document = printDoc;
                DialogResult result = printDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    printDoc.Print();
                }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to print selected text of richtextbox?

    Your code prints a blank pages because you are telling it to print a blank page (i.e. your printDoc variable is initialized to a default class which doesn't know what to print).

    You need to override PrintPageEventHandler as shown in the msdn PrintDocument class.

    Scroll down in the link for an example where a txt file is read by a StreamReader (the variable's name is streamToPrint) and this stream is printed in the pd_printPage event handler.

    Your code would implement the same event handler, but it would use the text from the richeditcntl (or the selected text of the control).

    I would recommend that you code up the sample using the txt file approach and get it to print the file and then modify the sample to print the contents of a richeditctrl.

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

Featured