none
TreeView Node Selection based on Fullpath RRS feed

  • Question

  • Hello ,

    I am working on Windows form and I have 2 Treeview controls. If I select a Node in TreeView1 then Same Node in TreeView2 should be selected(if present).

    So here is how I have written a code.

    tvPD1 is first Treeview and tvPD2 is second TreeView Control.

            private void tvPD1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                string leftTVFullPath = e.Node.FullPath;
           
                GetNodeFromPath(tvPD2.Nodes, leftTVFullPath);
            }
     
            public TreeNode GetNodeFromPath(TreeNodeCollection nodes, string path)
            {
                TreeNode foundNode = null;
                foreach (TreeNode tn in nodes)
                {
                    if (tn.FullPath == path)
                    {
                        tvPD2.SelectedNode = tn;
                        tvPD2.SelectedNode.EnsureVisible();  
                        tvPD2.Focus();
                        return tn;
                    }
                    else if (tn.Nodes.Count > 0)
                    {
                        foundNode = GetNodeFromPath(tn.Nodes, path);
                    }
                    if (foundNode != null)
                        return foundNode;
                }
                return null;
            }

    The main Problem with the above code is ..If there are two or more elements with the same name at the same level in the tree, this will return the first of the elements. How can we implement such function ?

    Regards

    Vyasa

    Wednesday, January 23, 2013 10:32 AM

Answers

  • Hi

    FarnhamSurrey

    this may be useful to you...atleast you will get some idea.....

    http://stackoverflow.com/questions/12234729/treeview-how-to-expand-a-fullpath-c-sharp


    • Edited by love4csharp Thursday, January 24, 2013 6:04 AM
    • Proposed as answer by Jason Dot Wang Friday, January 25, 2013 5:55 AM
    • Marked as answer by Jason Dot Wang Thursday, January 31, 2013 8:05 AM
    Thursday, January 24, 2013 6:00 AM
  • Before you add new Node, check for duplicate names and if needed set new Node.Name to something unique - now you can use Find method to find correct Node.

    i.e.

    // example: new node name string TreeNodeText = "newNode"; string TreeNodeName = TreeNodeText; int nameIndex = 0; while (Treeview1.Nodes.Find(TreeNodeName, true).Length > 0 || Treeview2.Nodes.Find(TreeNodeName, true).Length > 0) { TreeNodeName = TreeNodeText + nameIndex.ToString(); nameIndex++; }

    // now you can add new Node to Treeview1, Treeview2 or both... TreeNode newNode = Treeview2.Nodes.Add(TreeNodeText);

    // ...but with different identifier. newNode.Name = TreeNodeName;

    To find corresponding Node:

    TreeNode selNode = Treeview1.SelectedNode; TreeNode[] muchingNodes = Treeview2.Nodes.Find(selNode.Name, true);

    // ...add i.e. select... if (muchingNodes.Length > 0) Treeview2.SelectedNode = muchingNodes[0];



    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |





    • Edited by VendorX Thursday, January 24, 2013 11:40 AM
    • Marked as answer by Jason Dot Wang Thursday, January 31, 2013 8:05 AM
    Thursday, January 24, 2013 11:04 AM

All replies

  • If I understand you correctly, you are saying you could have  a treeview like this:

    a
      b
        c
      b
        d

    With two nodes at the second level called "b". Let me know if my understanding is incorrect, but if I am right, I suppose you could try using the Index property of TreeNode rather than FullPath. If it is not a business requirement to have duplicate names for nodes, you couilid also require the nodes to have unique names, thereby avoiding this issue

    Wednesday, January 23, 2013 10:38 AM
  • Ya, your understanding is correct but the thing is the order of 2 treeviews is not same. So there i will have problem again.

    eg:

    Treeview1                 TreeView2

    a                           a
      b                            b
        c                             e
      b                            b
        d                             c
      b                            b
        e                             d

    In these case how can I select the node ?

    • Proposed as answer by love4csharp Thursday, January 24, 2013 6:02 AM
    • Unproposed as answer by love4csharp Thursday, January 24, 2013 6:03 AM
    Thursday, January 24, 2013 4:41 AM
  • Hi

    FarnhamSurrey

    this may be useful to you...atleast you will get some idea.....

    http://stackoverflow.com/questions/12234729/treeview-how-to-expand-a-fullpath-c-sharp


    • Edited by love4csharp Thursday, January 24, 2013 6:04 AM
    • Proposed as answer by Jason Dot Wang Friday, January 25, 2013 5:55 AM
    • Marked as answer by Jason Dot Wang Thursday, January 31, 2013 8:05 AM
    Thursday, January 24, 2013 6:00 AM
  • Using Index to identify the node could still work for you here
    Thursday, January 24, 2013 8:25 AM
  • Before you add new Node, check for duplicate names and if needed set new Node.Name to something unique - now you can use Find method to find correct Node.

    i.e.

    // example: new node name string TreeNodeText = "newNode"; string TreeNodeName = TreeNodeText; int nameIndex = 0; while (Treeview1.Nodes.Find(TreeNodeName, true).Length > 0 || Treeview2.Nodes.Find(TreeNodeName, true).Length > 0) { TreeNodeName = TreeNodeText + nameIndex.ToString(); nameIndex++; }

    // now you can add new Node to Treeview1, Treeview2 or both... TreeNode newNode = Treeview2.Nodes.Add(TreeNodeText);

    // ...but with different identifier. newNode.Name = TreeNodeName;

    To find corresponding Node:

    TreeNode selNode = Treeview1.SelectedNode; TreeNode[] muchingNodes = Treeview2.Nodes.Find(selNode.Name, true);

    // ...add i.e. select... if (muchingNodes.Length > 0) Treeview2.SelectedNode = muchingNodes[0];



    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |





    • Edited by VendorX Thursday, January 24, 2013 11:40 AM
    • Marked as answer by Jason Dot Wang Thursday, January 31, 2013 8:05 AM
    Thursday, January 24, 2013 11:04 AM