Click here to Skip to main content
15,867,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to write multiple line in treeview's node:-

Example:Image[^]
Posted
Updated 16-Mar-20 19:08pm
v3

As you have found out, there is no direct way to have text with multiple lines displayed as the content of a WinForms' TreeView TreeNode.

Theoretically you could do this by custom owner-drawing the Node and its Text, and Microsoft has supplied an example of this (but, the example does not demonstrate multiple lines of text) since FrameWork 2.0: [^].

I would suggest you not waste the time to try and create custom owner-drawn nodes unless: you are an experienced developer, and you have a lot of time to waste for little return.

So, what are the alternatives ?

1. set the 'Scrollable property of the TreeView to 'true, and let the end-user scroll to see extended text content in a TreeNode.

2. set 'Scrollable to 'false and 'ShowNodeToolTips to 'true, and assign strings with line-break characters to the ToolTip property of all, or some, TreeNodes. Or, you could write some code that at application launch recursively went through all the TreeNodes, and created custom ToolTips for only those TreeNodes that you somehow figured out had extended length Text. The problem being there's no way to detect exactly how much of the current Text in the Node is not visible given the TreeView's width.

Finally, I'd suggest you examine the various TreeView, and TreeListView, controls that have been published here on CP over the years, starting with Philip Piper's classic 'ObjectListView, which has been evolving for many years, and which is still actively supported (as of May, 2014): [^].

I believe, but am not sure, that 'ObjectListView supports multi-line text content in Nodes.
 
Share this answer
 
Comments
Master@Infinity 13-May-14 5:58am    
have seen some example of "DrawNode" before but none are working fine in my case and i am willing to waste some time on it.
Master@Infinity 13-May-14 6:00am    
and btw what is "ObjectListView"? can you give me some example of an article or code?
Hi i am giving u method only to know how to do this ok its only example i am not testing yet this code is work or not......


private void Form1_Load(object sender, EventArgs e)
{
//
// This is the first node in the view.
//
TreeNode treeNode = new TreeNode("BMW");
treeView1.Nodes.Add(treeNode);
//
// Another node following the first node.
//
treeNode = new TreeNode("Maruti");
treeView1.Nodes.Add(treeNode);
//
// Create two child nodes and put them in an array.
// ... Add the third node, and specify these as its children.
//
TreeNode node2 = new TreeNode("Boloro");
TreeNode node3 = new TreeNode("Xylo");
TreeNode[] array = new TreeNode[] { node2, node3 };
//
// Final node.
//
treeNode = new TreeNode("Mahindra", array);
treeView1.Nodes.Add(treeNode);
}
 
Share this answer
 
Comments
Master@Infinity 10-May-14 4:38am    
sir, actually i don't get it you are just adding "Nodes to the tree". I need new lines in the same node not different node. Please see the image again sir.
Try this
TreeNode tnode_child_item = new TreeNode();
tnode_child_item.Text = "<span> <div > first line<br/> <span>second line</span>  </div></span>";


output will be like
+ first line
second line
 
Share this answer
 
Comments
Richard Deeming 17-Mar-20 7:33am    
The question is about the Windows Forms TreeView. Your solution appears to be for the Web Forms TreeView, which is a completely different control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900