CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2004
    Posts
    8

    Question How to detece a duplicate node while adding a new node to a tree list view control

    Hello Gururs,
    can any one of you send me the code for :
    How to detect a duplicate node in existing while adding a new node to a tree list view control in c#. or

    Validation mechanism for duplicate nodes in tree view while adding new node.

    Thanks,
    Ranga,
    Last edited by dwaipeyan; March 5th, 2009 at 04:55 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: How to detece a duplicate node while adding a new node to a tree list view contro

    What is your definition of 'duplicate node'.

    You could use the treenode.Nodes.ContainsKey method

    for example


    Code:
    //supose whe have a Person class with properties Username, Firstname and Lastname (all string)
    
    List<Person> persons = new List<Person>();
    persons.Add(new Person("username1", "firstname1", "lastname1"));
    persons.Add(new Person("username2", "firstname2", "lastname2"));
    persons.Add(new Person("username1", "firstname1", "lastname1"));
    
    foreach(Person p in persons){
      if (! treeview1.Nodes.ContainsKey(p.Username)){
         treeview1.Nodes.Add(p.Username, string.Format("{0} {1}", p.Firstname, p.Lastname));
      }
    }
    After the executing of this code you only will have 2 persons in the treeview

  3. #3
    Join Date
    Sep 2004
    Posts
    8

    Re: How to detece a duplicate node while adding a new node to a tree list view contro

    Hi,

    Thank you very much for your solution.

    But the tree control what I am using does not have this property method.
    I am using DevX Tree view control.

    Thanks

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to detece a duplicate node while adding a new node to a tree list view contro

    I'd use a container that doesn't allow duplicates and then bind the container to the control.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to detece a duplicate node while adding a new node to a tree list view contro

    If you are using the TreeView in unbound mode, you could create your own TreeNode class and define your own version of equality. If you are using data binding, take Arjay's advice.

  6. #6
    Join Date
    Sep 2004
    Posts
    8

    Re: How to detece a duplicate node while adding a new node to a tree list view contro

    Hi all,

    Thank you for your informations

    I will try and update you

    Thanks,
    Ranga

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured