"Contains" method for custom TreeNode

  • Thread starter Thread starter Joel Lyons
  • Start date Start date
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?!
 
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
 
Back
Top