Christopher Pepin
Christopher Pepin

Reputation: 21

How to stop Treenode text from being overwitten?

I am having problems where the text displayed for my treenodes is being replaced by a reference to the class I am storing in the tag property.

I have a simple form I have built using the form designer in Visual Studio 2019 that has a treeview control that I populate using with treeNodes. I have a custom class called LineItem that stores all the information associated with each node that I store in the tag. I then set the text property of the node using the ToString function I set up in the class. The result however has been that every node ends up displaying the text PlankForm.LineItem, PlankForm being the project name. Below is the code I am currently using to add the node.

//Create new node in level match group with appropriate LineItem
TreeNode treeNode = new TreeNode();
treeNode.Tag = lineItem;
int index = lineItemTree.Nodes[lineItem.levelMatch].Nodes.Add(treeNode);
lineItemTree.Nodes[lineItem.levelMatch].Nodes[index].Text = lineItem.ToString();

I have tried setting the nodes text property both before and after adding it to the tree but both seems to have the same result.

Upvotes: 1

Views: 62

Answers (1)

Christopher Pepin
Christopher Pepin

Reputation: 21

The problem is in the ToString function. It was written in the code as a property named ToString instead of an override of the ToString function.

Upvotes: 1

Related Questions