Results 1 to 5 of 5

Thread: [RESOLVED] How to get node index of treeview branch

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    57

    Resolved [RESOLVED] How to get node index of treeview branch

    After searching most of the treeview node posts I have not been able to find the answer I am looking for.

    I have a treeview loaded like the following example:
    Parent1
    Child1
    GrandChild1
    GreatGrandChild1
    GreatGrandChild2
    GreatGrandChild3
    GrandChild2
    GreatGrandChild1
    GreatGrandChild2
    Child2
    GrandChild1
    GreaGrandChild1
    Parent2
    Child1
    GrandChild1
    GreatGrandChild1
    GreatGrandChild2
    GreatGrandChild3
    GrandChild2
    GreatGrandChild1
    GreatGrandChild2
    Child2
    GrandChild1
    GreaGrandChild1

    When I select a node I need to display data from a database>table> record specific to that node.

    The database has four tables. Parent, Child, GrandChild and GreatGrandChild.

    Each record in the Parent table has the following fields:
    ParentNodeIdx, ParentData

    Each record in the Child table has the following fields:
    ParentNodeIdx, ChildNodeIdx, ChildData

    Each record in the GrandChild table has the following fields:
    ParentNodeIdx, ChildNodeIdx, GrandChildNodeIdx, GrandChildData

    Each record in the GreatGrandChild table has the following fields:
    ParentNodeIdx, ChildNodeIdx, GrandChildNodeIdx, GreatGrandChildNodeIdx, GreatGrandChildData

    My problem is trying to figure out what the node indexes are for the Parent and Child Nodes when I mouse click on the GreatGrandChild node. I use the Treeview1.SelectedNode.Index to get the GreatGrandChildNode Index and the Treeview1.SelectededNode.Parent.Index to get the GrandChildnode index but can't figure out how to get the Child and Parent Node indexes.

    Sorry for the long post but I tried to make my problem as clear as I could.

    Thanks for any and all suggestions
    Richard

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to get node index of treeview branch

    Each TreeNode object has a Tag property, so you can store arbitrary information there. When the user selects a node, you can get its Tag data and then get the next node up the chain from its Parent property. Keep doing that up the chain until the Parent is Nothing and you know that you're at the top level.

    You could even define your own class(es) derived from TreeNode and add your own properties to store your own data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: How to get node index of treeview branch

    This article talks about extending the TreeNode class like hmcilhinney suggested: How to create a Key property for a TreeView node in Visual Basic.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    57

    Re: How to get node index of treeview branch

    I'll try the Tags idea and see if I can make that work. Thanks for both replies.

    Richard

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    57

    Re: How to get node index of treeview branch

    The Tags idea solved my problems. Thanks again!

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