PC Review


Reply
Thread Tools Rate Thread

"Contains" method for custom TreeNode

 
 
Joel Lyons
Guest
Posts: n/a
 
      11th Aug 2003
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?!


 
Reply With Quote
 
 
 
 
Joel Lyons
Guest
Posts: n/a
 
      11th Aug 2003
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

"Mattias Sjögren" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Joel,
>
> >What am I doing wrong?!

>
> Nothing. But it looks like the implementation of
> TreeNodeCollection.Contains() only checks for reference equality, it
> never calls Equals(). I guess you have to maintain a separate list of
> nodes and check that one instead to avoid duplicates.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/
> Please reply only to the newsgroup.



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to update "Custom Priority" or "Custom Status" Task fields? bpeery Microsoft Outlook Discussion 1 1st Dec 2009 01:59 AM
How to detect if I use "AES" or "TKIP" as encryption method for "WPA-PSK"? Client or AP requirement? Kevin McNeal Windows XP Networking 2 27th Feb 2009 10:30 AM
What is Error "Method "Paste" of object "_Worksheet" failed? =?Utf-8?B?dmF0?= Microsoft Excel Programming 7 17th Feb 2006 08:05 PM
<FORM METHOD="post" onSubmit="return fieldcheck()" name="orientation" action="http://ws-kitty.BU.edu/AT/survey/orientation/script/write.asp" language="JavaScript"> Joeyej Microsoft ASP .NET 0 4th Jun 2004 08:55 PM
Howto customize "Add Method" wizard on "Add Method" menu of Class View? Bill Smarty Microsoft Dot NET Framework 0 13th Jan 2004 06:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 PM.