Results 1 to 4 of 4

Thread: Please ammend this code of TreeView

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52

    Please ammend this code of TreeView

    Hi,

    in the following code I can create a TreeView control with only one Root and only one branch. Please change or ammend this code to be able to make the branch as a root of another branch and so on.. I mean Treeview with unlimited levels.
    You can suggest any new variables or objects

    Do While Not rsCategories.EOF
    Me.ctlTreeView.Nodes.Add , , "Cat" & rsCategories("CategoryID"), rsCategories("CategoryName") ', "category", "category"
    rsCategories.MoveNext
    Loop

    Do While Not rsProducts.EOF
    Me.ctlTreeView.Nodes.Add "Cat" & rsProducts("categoryID"), tvwChild, "Prod" & rsProducts("productid"), rsProducts("ProductName") ' "product", "product"
    rsProducts.MoveNext
    Loop


    Thanx in advance

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    In order to add multiple nodes you will need to make sure that your key values are unique. Here is one way of doing it.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim strParentKey As String
    4.    
    5.     On Error GoTo ErrorRoutine
    6.  
    7.     TreeView1.Nodes.Add , , UniqueKey, "one"
    8.     strParentKey = TreeView1.Nodes(TreeView1.Nodes.Count).Key
    9.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "two"
    10.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "three"
    11.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "Four"
    12.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "Five"
    13.  
    14.     Exit Sub
    15.  
    16. ErrorRoutine:
    17.  
    18.     If Err.Number = 35602 Then
    19.         ' Duplicate key, get a different one
    20.         Resume
    21.     End If
    22.  
    23.    
    24. End Sub
    25.  
    26.  
    27. Public Function UniqueKey() As String
    28.  
    29.     UniqueKey = "K" & 1 + Int(Rnd() * 10000000)
    30.  
    31. End Function

    You can also put some more info in the key like


    TreeView1.Nodes.Add , , "category" & UniqueKey, "one"

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52
    Hi Martin

    Thanx for answering. I need many steps , I mean let "Two" like a Root and branch at the same time. how can I do that

    Mina

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    It's pretty much the same principle. You just need to refer to the parent.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Dim strParentKey As String
    4.     Dim strTwoKey As String
    5.     Dim strTwo_bKey As String
    6.    
    7.     On Error GoTo ErrorRoutine
    8.  
    9.     TreeView1.Nodes.Add , , UniqueKey, "one"
    10.     strParentKey = TreeView1.Nodes(TreeView1.Nodes.Count).Key
    11.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "two"
    12.     strTwoKey = TreeView1.Nodes(TreeView1.Nodes.Count).Key
    13.     TreeView1.Nodes.Add strTwoKey, tvwChild, UniqueKey, "two-a"
    14.     TreeView1.Nodes.Add strTwoKey, tvwChild, UniqueKey, "two-b"
    15.     strTwo_bKey = TreeView1.Nodes(TreeView1.Nodes.Count).Key
    16.     TreeView1.Nodes.Add strTwo_bKey, tvwChild, UniqueKey, "two-b-1"
    17.     TreeView1.Nodes.Add strTwoKey, tvwChild, UniqueKey, "two-c"
    18.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "three"
    19.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "Four"
    20.     TreeView1.Nodes.Add strParentKey, tvwChild, UniqueKey, "Five"
    21.  
    22.     Exit Sub
    23.  
    24. ErrorRoutine:
    25.  
    26.     If Err.Number = 35602 Then
    27.         ' Duplicate key, get a different one
    28.         Resume
    29.     End If
    30.    
    31. End Sub

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