How to print a TreeView?
i wanna to print the data that displayed in a treeview when the user click on Print Button by using Print Document and p开发者_开发问答rint it in actual paper i am using Visual Studio 2008
Try if these links are of any help:
http://bytes.com/topic/c-sharp/answers/886644-printing-treeview
https://stackoverflow.com/questions/5602383/want-to-print-data-in-treeview-in-c
Create a in memory TreeView and then copy the significant properties and transfer a root node. After do that create a in memory bitmap and draw a Treeview content by DrawToBitmap Function, draw a image on page and restore root node to original place.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var memtree = new TreeView();
var rn = treeView1.Nodes[0];
treeView1.Nodes.Remove(rn);
memtree.ImageList = treeView1.ImageList;
memtree.BorderStyle = BorderStyle.None;
memtree.Nodes.Add(rn);
memtree.ClientSize = new Size(e.MarginBounds.Width, e.MarginBounds.Height);
var bmp = new Bitmap(e.MarginBounds.Width, e.MarginBounds.Height);
memtree.DrawToBitmap(bmp, new Rectangle(0, 0, e.MarginBounds.Width-1, e.MarginBounds.Height-1));
e.Graphics.DrawImage(bmp, e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width -1, e.MarginBounds.Height -1);
memtree.Nodes.Remove(rn);
treeView1.Nodes.Add(rn);
}
精彩评论