Looping through TreeView Nodes?

Last post 03-23-2009 3:46 AM by darkcat02. 4 replies.

Sort Posts:

  • Looping through TreeView Nodes?

    09-11-2007, 12:35 PM

    Hello,

    I'm using the Microsoft Web Control.  I'm dynamically loading the control with data from the database where there are parent nodes and children nodes that could be up to 5 levels.  I have a Javascript function that executes when a parent node is checked to automatically select all the children below it.  Now I have to figure out a way to find out what nodes are selected so that I can process them properly.  The only way I see this happening is if I create a hidden variable and loop through all the nodes in the TreeView and add the nodes to the variable if they are checked.  However, I'm having a problem looping though the nodes.  The only information I found was the SelectNodeIndex property which just gives me the node that was selected.  

    This is my TreeView Format:

    • Folder10  
      • Folder1
        • Item1
      • Folder2
        • Item2
    • Folder 11
      • Folder 3
        • Item4
        • Item5
    • Folder 12
      • Folder4
        • Folder5
          • Folder6
            • Item6
    • Folder 13
      • Item7

     

    I need to find out which Item was selected in the TreeView.  So lets say that I checked the Folder10 and Folder12 (all the subfolders and items are selected automatically).  Then I want the hidden variable to hold all the selected nodes in this format;

    Variable = Folder10_1_item1 (So the each parent node selected would start of with text "Folder" then the ID of the folder, then an "_" which means there is a child. Then the ID of the subFolder and then the text "item" and then the ID)

     So If I selected Folder10 and Folder12 in the TreeView, I would like the variable to hold this information

    Variable = Folder10_1_item1;Folder10_2_item2;Folder12_4_5_6_Item6

     Can someone please help?  How can I loop through each node of the treeview to achieve this.

     Thank you

  • Re: Looping through TreeView Nodes?

    11-09-2007, 7:42 PM
    • Member
      4 point Member
    • bsharp3
    • Member since 10-30-2007, 3:31 AM
    • Titusville, FL
    • Posts 2

    Here's a couple of subs I put together to loop through all treenodes.  I had to loop through the tree for a node with a specific value to select and expand it.  hope this helps.

     

    Private Sub RecurseNodes(ByVal searchValue As Integer)

    For Each tn As TreeNode In TreeView1.Nodes

    If tn.Value = searchValue Then

    tn.Expand()

    tn.Select()

    Exit For

    End If

    If tn.ChildNodes.Count > 0 Then

    For Each cTn As TreeNode In tn.ChildNodes

    recurseChildren(cTn, searchValue)

    Next

    End If

    Next

    End Sub

     

    Private Sub recurseChildren(ByVal tn As TreeNode, ByVal searchValue As Integer)

    If tn.Value = searchValue Then

    tn.Expand()

    tn.Select()

    Exit Sub

    End If

    If tn.ChildNodes.Count > 0 Then

    For Each tnC As TreeNode In tn.ChildNodes

    recurseChildren(tnC, searchValue)

    Next

    End If

    End Sub

  • Re: Looping through TreeView Nodes?

    11-12-2007, 4:56 PM
    • Member
      9 point Member
    • Rupen Shah
    • Member since 06-26-2007, 9:17 PM
    • Posts 25

    I found that there is a property in the treeview CheckedNodes which gives list of nodes that are checked. Is there a opposite property which gives unchecked nodes from the treeview?

  • Re: Looping through TreeView Nodes?

    11-25-2007, 7:28 AM
    • Member
      4 point Member
    • asmvax
    • Member since 11-25-2007, 12:24 PM
    • Posts 2

     i've made some changes in your code:

    you will call with  SelectNodesRecursive(what node to select ex:"10", name of the treeview ex: treeview1);

     

    public static void SelectNodesRecursive(string searchValue,TreeView Tv)
        {
            foreach (TreeNode tn in Tv.Nodes)
            {
                if (tn.Value == searchValue)
                {
                    tn.Expand();
                    tn.Select();
                    break;
                }

                if (tn.ChildNodes.Count > 0)
                {
                    foreach (TreeNode cTn in tn.ChildNodes)
                    {
                        int a = SelectChildrenRecursive(cTn, searchValue);
                        if (a == 1)
                        {
                            tn.Expand();
                        }
                    }
                }
            }
        }


        private static int SelectChildrenRecursive(TreeNode tn, string searchValue)
        {
            if (tn.Value == searchValue)
            {
                //if(tn.Parent.Parent!=null)
                //{
                // tn.Parent.Parent.Expand();
                //}
                //if (tn.Parent != null)
                //{
                //    tn.Parent.Expand();
                //}

                tn.Expand();
                tn.Select();
                return 1;
            }
            else
            {
                // tn.Parent.Collapse();
                tn.Collapse();
            }
            if (tn.ChildNodes.Count > 0)
            {
                foreach (TreeNode tnC in tn.ChildNodes)
                {
                    int a = SelectChildrenRecursive(tnC, searchValue);
                    if (a == 1)
                    {
                        tn.Expand();
                        return 1;
                    }

                }

            }
            return 0;
        }

     

  • Re: Looping through TreeView Nodes?

    03-23-2009, 3:46 AM
    • Member
      178 point Member
    • darkcat02
    • Member since 05-05-2008, 10:27 PM
    • Manila, Philippines
    • Posts 151

     hi there...

     can you give me an example how did you execute those functions? i need it badly.. thanks a lot... 

    Regards,
    Mhaey

    Please remember to click “Mark as Answer” on the post that helps you.. =)
Page 1 of 1 (5 items)