Check node exist in treeview

13 posts, 1 answers
  1. SachinC
    SachinC avatar
    23 posts
    Member since:
    Sep 2010

    Posted 15 Oct 2010 Link to this post

    Hello,

    i am having a treeview. and adding the nodes manually.
    For Each ct In ChecklistTemplate
      
                    newChecklistTypeNode = New RadTreeNode(ct.ChecklistTypeName, False)
                    newChecklistTypeNode.Tag = ct.CheckistTypeID
                    newChecklistTypeNode.LoadedOnDemand = True
      
                    If (Not tvChecklists.Nodes.Contains(newChecklistTypeNode)) Then
                        tvChecklists.Nodes.Add(newChecklistTypeNode)
                    End If
    Next

    i am checking whether the node is already present in the treeview.
    suppose following nodes are already added in treeview
    1. abc
    2. xyz
    3. pqr

    if i now add xyz node in treeview the condition tvChecklists.Nodes.Contains(newChecklistTypeNode) returns FALSE.

    Please suggest.

    Thanks
    Sachin




  2. SachinC
    SachinC avatar
    23 posts
    Member since:
    Sep 2010

    Posted 15 Oct 2010 Link to this post

    Found the solution
    Change the code bit

    Dim newChecklistTypeNode As RadTreeNode = Nothing
                For Each ct In ChecklistTemplate
      
                    newChecklistTypeNode = New RadTreeNode(ct.ChecklistTypeName, False)
                    newChecklistTypeNode.Tag = ct.CheckistTypeID
                    newChecklistTypeNode.LoadedOnDemand = True
      
                    If (tvChecklists.Nodes.Item(ct.ChecklistTypeName) Is Nothing) Then
                        tvChecklists.Nodes.Add(newChecklistTypeNode)
                    End If
    Next

    Thanks!
    :)


  3. UI for WinForms is Visual Studio 2017 Ready
  4. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Oct 2010 Link to this post

    Hello Sachin,

    This should work:
    //treeView.Nodes.Find(string key, bool searchAllChildren)
    treeView.Nodes.Find(key, true)

    But I'm not 100%, usually, with trees, you have to parse them in order to find the node, something like this:
    private bool NodeExists(RadTreeNodeCollection levelNodes, string key)
    {
        var exists = false;
        foreach (RadTreeNode node in levelNodes)
        {
            if (node.Text == key)
            {
                exists = true;
                break;
            }
     
            exists = NodeExists(node.Nodes, key);
        }
     
        return exists;
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 15 Oct 2010 Link to this post

    Hi, 

    I think you're right about having to parse them. The Telerik response to this question can be found in this code library area
    Anyway, as far as I'm aware, a Find is usually doing the same thing under the hood. 
    All the best
    Richard
  6. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Oct 2010 Link to this post

    If it was working... just tried it, returns 0 elements all the time...

    So either the method i suggested, or an extension method if it is needed in more than one class (to avoid duplicate code).

    Best Regards,
    Emanuel Varga
  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 20 Oct 2010 Link to this post

    Hello guys, 

    Currently, there are two ways to find a node. The first is the one that Richard has pointed out and the other one is described in our product documentation.

    As to the Find method in RadTreeView that always returns 0 elements, this is a known issue that we are going to address in a future release. 

    Should you guys need any assistance, do not hesitate to contact me.
     
    Sincerely yours,
    Stefan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Mark de Torres
    Mark de Torres avatar
    58 posts
    Member since:
    Jan 2010

    Posted 25 Jan 2013 Link to this post

    Hi Stefan,

    The link is not working. Please advise


    Richard Slade,

    Your link is not working either. Please advise
  9. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Jan 2013 Link to this post

    Hi,

    Take a look into this documentation for getting information about searching for a single node by its text.

    Hope this helps.

    Regards,
    Princy.
  10. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 29 Jan 2013 Link to this post

    Hi guys,

    @Mark - please refer to the article Princy mentioned.

    @Princy - thank you for helping our the community. I am updating your Telerik points for the efforts.
     
    Regards,
    Stefan
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  11. Mark de Torres
    Mark de Torres avatar
    58 posts
    Member since:
    Jan 2010

    Posted 02 Feb 2013 Link to this post

    Hi Princy, Stefan

    Thank you
  12. David
    David  avatar
    20 posts
    Member since:
    Oct 2012

    Posted 14 Feb 2013 Link to this post

    Hi Emanuel,

    Thank you
  13. Mark de Torres
    Mark de Torres avatar
    58 posts
    Member since:
    Jan 2010

    Posted 15 Feb 2013 Link to this post

    public EntityCollection<MusicEntity> GetMusicData()
    {

        EntityCollection<MusicEntity> collection = GetMusicData();

       

        treMusic.Nodes.Clear();

        ArrayList ptnodes = new ArrayList();


        RadTreeNode nodeWorkplane = new RadTreeNode(p.PWork);
        RadTreeNode nodeUser = new RadTreeNode(p.UserProject.Items[0].UserName);

        RadTreeNode musicgroupnode = new RadTreeNode(p.MusicSubGroup.MusicGroup.Name, String.Empty);
        RadTreeNode musicsubgroupnode = new RadTreeNode(p.MusicSSubGroup.Name, String.Empty);
        RadTreeNode musicnode = new RadTreeNode(p.MusicTitle);
        musicsubgroupnode.Nodes.Add(musicnode);
        musicgroupnode.Nodes.Add(musicsubgroupnode);


        nodeUser.Nodes.Add(musicgroupnode);
        nodeUser.Nodes.Add(nodeWorkplane);


        pgnodes.Add(nodeUser);

        foreach (RadTreeNode pg in ptnodes) 
        {
            treMusic.Nodes.Add(pg);

        }

    }

    currently nodeUser the same user more than one.How i should prevent to add the same user node. please advise.
  14. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 19 Feb 2013 Link to this post

    Hi David,

    RadTreeView has a Nodes collection where the root nodes are placed and additionally each RadTreeNode also has Nodes collection for its child nodes, so you will need a recursive function to iterate the nodes. Here you can find a example: http://www.telerik.com/community/code-library/winforms/general/get-a-flattened-list-of-all-radmenuitems-in.aspx.

    I hope this helps.

    All the best,
    Stefan
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Back to Top
UI for WinForms is Visual Studio 2017 Ready