Results 1 to 5 of 5

Thread: [RESOLVED] Question about node of treeview.

  1. #1

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Resolved [RESOLVED] Question about node of treeview.

    How can i know selectednode of treeview is rootnode or childnode or child of childnode.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

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

    Re: A good question about node of treeview.

    Have you read the documentation for the TreeNode class? If you have then you know what properties it has so you know the answer to your question.
    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

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Question about node of treeview.

    I read that document so i can solve that problem like this :

    Code:
    Dim Count As Integer = mytreeview.SelectedNode.GetNodeCount(True)
    if count = 0 then
    Msgbox(mytreeview.SelectedNode.FullPath & " is last child node")
    End if
    But i want to check all nodes for determine what class of node does it belong to ?
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

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

    Re: Question about node of treeview.

    So, are you saying that you don't know how to traverse the tree? If so then the answer is with recursion. A tree is an inherently recursive structure so recursion is the best way to traverse a tree is using recursion. In the case of a TreeView you can do it like so:
    vb.net Code:
    1. Private Sub ProcessNodes(ByVal nodes As TreeNodeCollection)
    2.     For Each node As TreeNode In nodes
    3.         'Process node, e.g.
    4.         MessageBox.Show(node.FullPath)
    5.  
    6.         'Process child nodes.
    7.         Me.ProcessNodes(node.Nodes)
    8.     Next
    9. End Sub
    Obviously displaying the full path in a message box is just an example but you can process the node in any way you see fit. You would start the ball rolling by call that method and passing the Nodes property of your TreeView. It will then visit every node in the tree.

    Note that that code will perform a depth-first search, i.e. the node's children are processed before it siblings. It's the most natural so it should be your first choice. If you specifically need a breadth-first search though:
    vb.net Code:
    1. Private Sub ProcessNodes(ByVal nodes As TreeNodeCollection)
    2.     For Each node As TreeNode In nodes
    3.         'Process node, e.g.
    4.         MessageBox.Show(node.FullPath)
    5.     Next
    6.  
    7.     For Each node As TreeNode In nodes
    8.         'Process child nodes.
    9.         Me.ProcessNodes(node.Nodes)
    10.     Next
    11. End Sub
    You might also want to take note of the TreeNode.Level property, which is included in the documentation.
    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

  5. #5

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Question Re: Question about node of treeview.

    Quote Originally Posted by jmcilhinney View Post
    You might also want to take note of the TreeNode.Level property, which is included in the documentation.
    That is which i want to. and i did it. thanks .
    Last edited by manhit45; Jul 20th, 2009 at 02:16 AM.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

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