Search code examples
c#treeviewrichtextboxrtf

Treeview copy to RichtextBox or print


I've a TreeView (directory tree) and like to 'copy' the checked (checkboxes) nodes to a RichTextBox or other control (In the end it needs to be printed).

So the result is a custom tree (text and icons) which can be printed.

Is this possible?

I couldn't find any usefull information with Google!

( Language: C# )


Solution

  • What's a problem? Create new TreeView and copy resursively checked nodes from source TreeView to created one. Then use Control.DrawToBitmap() and print obtained bitmap. For example:

    void PrintNewTreeView()
    {
        var pd = new PrintDocument();
        pd.PrintPage += OnPrintPage;
        pd.Print(); 
    }
    
    void OnPrintPage(object sender, PrintPageEventArgs e)
    {
        var bitmap = new Bitmap(newTreeView.Bounds.Size);
        newTreeView.DrawToBitmap(bitmap, bitmap.Size);
        var pt = Point.Empty; // drawing origin
        e.Graphics.DrawImage(bitmap, pt);
    }