How to get Tree nodes in hierarchical order ?

May 13, 2009 at 6:24 AM

Dear All,

 

I’ve a tree with few tree nodes in hierarchical order.

 

But Tree.Nodes is returning all the nodes in the sequential order.

 

Is there anyway to get the nodes in the hierarchical order ?

If so please let me know ! Thank You !

 

Thanks & Regards,

Kannan

Coordinator
May 25, 2009 at 8:16 AM

When you say sequential order, do you mean that it is finding the nodes as you navigate the hierarchy.

Are you looking for more of a disconnected data structure?

May 25, 2009 at 9:13 AM

Hi Vivek,

 

For example, I've a tree like this,

 

RootNode

     !------------> Parent1

                            !--------> Parent2

                                               !--------Child1

     !------------> Node1

 

Now the Tree.Nodes property has five child nodes like this in the sequential order,

RootNode

Parent1

Parent2

Child1

Node1

 

Parent1 and Parent2 don’t have any child Nodes . . . Even in UISpy, it is displayed like this :-(

 

Thanks & Regards,

Kannan

May 25, 2009 at 9:28 AM

Hi Vivek,

 

            It works fine with Windows TreeView item.

 

But I’m using Infragistics UltraTree, where it is not working and have the behavior as specified previously . . .

 

So I need to find a workaround . . .

 

Anyway Sorry and Thanks . . .

 

Thanks & Regards,

Kannan

May 26, 2009 at 4:26 PM

Hi,

 

I found a workaround to get the TreeNodes in hierarchical structure for Infragistics UltraTree.

 

The clue is the Value of the TreeNode which represents the level of the tree node wrt to root node.

 

So for the above example,

The value of RootNode is 0

The value of Parent1 is 1

The value of Parent2 is 2

The value of Child1 is 3

The value of Node1 is 1

 

The following code will help to iterate the nodes in hierarchical manner,

 

        private TreeNodes _allNodes;

 

        private TreeNodes _rootNodes;

        /// <summary>

        /// List of all nodes

        /// </summary>

        public virtual TreeNodes AllNodes

        {

            get

            {

                ConstructTree();

 

                return _allNodes;

            }

  }

 

        /// <summary>

        /// List of all root nodes

        /// </summary>

        public virtual TreeNodes Nodes

        {

            get

            {

                ConstructTree();

 

                return _rootNodes;

            }

        }

 

  private void ConstructTree()

        {

            TreeNodes treeNodes = new TreeNodes(finder, this);

 

            TreeNodes nodes = new TreeNodes();

 

            if (treeNodes.Count == 0)

            {

                _allNodes = treeNodes;

                _rootNodes = nodes;

 

                return;

            }

 

            TreeNode node = new Win32TreeNode(treeNodes[0].AutomationElement, this);

 

            nodes.Add(treeNodes[0]);

 

            int index = 0;

 

            while (true)

            {

                index++;

 

                if (index >= treeNodes.Count)

                {

                    break;

                }

 

                AddChildren(treeNodes, treeNodes[0], 1, ref index);

            }

 

            _allNodes = treeNodes;

            _rootNodes = nodes;

        }

 

  private void AddSiblings(TreeNodes treeNodes, TreeNode node, int value, ref int index)

        {

            TreeNode childNode = treeNodes[index];

 

            node.ChildNodes.Add(childNode);

 

            childNode.Parent = node;

 

            index++;

 

            if (index >= treeNodes.Count)

            {

                return;

            }

 

            TreeNode nextNode = treeNodes[index];

 

            ValuePattern valuePattern = nextNode.AutomationElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

 

            int nextValue = int.Parse(valuePattern.Current.Value);

 

            if (nextValue == value)

            {

                AddSiblings(treeNodes, node, nextValue, ref index);

            }

            else if (nextValue > value)

            {

                AddChildren(treeNodes, childNode, nextValue, ref index);

            }

            else

            {

                index--;

            }

        }

 

        private void AddChildren(TreeNodes treeNodes, TreeNode node, int value, ref int index)

        {

            TreeNode childNode = treeNodes[index];

 

            node.ChildNodes.Add(childNode);

 

            childNode.Parent = node;

 

            index++;

 

            if (index >= treeNodes.Count)

            {

                return;

            }

 

            TreeNode nextNode = treeNodes[index];

 

            ValuePattern valuePattern = nextNode.AutomationElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;

 

            int nextValue = int.Parse(valuePattern.Current.Value);

 

            if (nextValue == value)

            {

                AddSiblings(treeNodes, node, nextValue, ref index);

            }

            else if (nextValue > value)

            {

                AddChildren(treeNodes, childNode, nextValue, ref index);

            }

            else

            {

                index--;

            }

        }

 

Hope this may help someone . . .

 

Thanks & Regards,

Kannan