"Contains" method for custom TreeNode

J

Joel Lyons

I have TreeNode-derived objects (MyTreeNode) that I store in a TreeView.
Before I add new nodes, I want to make sure a node with the same identity
(Text property) isn't already there. So, I overrode Object.Equals in a
MyTreeNode class like so:

public class MyTreeNode : TreeNode
{
...
public override bool Equals(object obj)
{
return true; //TODO: compare Text property instead
}
}


Obviously, that's not all it should do, but the problem is this method never
even gets called!
I add the new node like so:


MyTreeNode node = new MyTreeNode("testing");
if(!tvMyTreeView.Nodes.Contains(node))
tvMyTreeView.Nodes.Add(node);


What am I doing wrong?!
 
J

Joel Lyons

Ouch - that's interesting, considering the docs say that CollectionBase uses
Equals to implement IList.Contains. I wonder why TreeNodeCollection doesn't
implement IList.Contains the same way.

Thanks for the help!

-Joel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top