2

I created a private variable

private TreeView currentFullTree;

After that I run through a database and create a nice treeview with parent, child, child nodes. That works fine also. Once the treeview is created I set my private variable to

currentFullTree = treeView1;

That populates perfectly fine and I can even extract each node and re-create the tree exactly how it looked originally from a different function.

Now this is where I'm getting lost. I've created a filter/search function separate from where I created the treeview or re-create the treeview. I want to only display the nodes that match the text provided. Before I perform the search I do a treeView1.Nodes.Clear(); so it looks nice and isn't cluttery.

When I do this, it also empties out my private variable I created! I'm not entirely sure why it is doing this. I was thinking since it is just a TreeView variable I would be able to store my treeview in there and access it as I please. I'm not sure if it's something silly I'm overlooking, but any help is appreciated.

Thanks, Tim

Timmy
  • 543
  • 1
  • 7
  • 19
  • 1
    _When I do this, it also empties out my private variable I created! I'm not entirely sure why it is doing this._ You will need to read up on "reference types" to understand that behaviour. Here's a good link: http://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c – Ulric Sep 09 '15 at 12:41
  • Thank you! That was very informative! – Timmy Sep 09 '15 at 13:11

2 Answers2

3

Your currentFullTree and treeView1 are pointing to the same object because those variables are of reference type.

Either you have to keep copy of whole tree or just tree nodes.

private TreeView currentFullTree;
private List<TreeNode> treeNodes;

and before clearing tree just add them to the list:

treeNodes.AddRange(treeView1.Nodes.OfType<TreeNode>());
treeView1.Nodes.Clear();

and when you want to show full tree (original) then add nodes from list back to tree view.

gzaxx
  • 17,312
  • 2
  • 36
  • 54
  • Thanks, that really helped out. This is the first time I've ever run into anything like this, so I was even struggling figuring out how to search what my problem is. – Timmy Sep 09 '15 at 13:13
2

Your variable is only a reference to an object.

You need to clone it to be able to have different treeview (one for reference and one for display).

see https://msdn.microsoft.com/en-us/library/ms173109.aspx

this may also be usefull for cloning the tree https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.clone%28v=vs.110%29.aspx

but be aware of this:

Remarks

The tree structure from the tree node being cloned and below is copied. Any child tree nodes assigned to the TreeNode being cloned are included in the new tree node and subtree.

The Clone method performs a shallow copy of the node. If the value of the Tag property is a reference type, both the original and cloned copy will point to the same single instance of the Tag value.

maazza
  • 7,016
  • 15
  • 63
  • 96