Results 1 to 4 of 4

Thread: [RESOLVED] Doubt about treeview and nodes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Resolved [RESOLVED] Doubt about treeview and nodes

    I have a doubt about using treeviews and nodes... I've been using the treeview with Java and C#, and when I worked with them I could easily "attach" some class to each node, and hence defining some custom properties for it (username, pass, address, connected, etc), and then while browsing the tree I was able to retrieve the type of the node, and using some specific functions of it.

    How could I easily do this? does anyone have any tip or suggestion for something like this?

  2. #2
    Lively Member
    Join Date
    Jan 1999
    Location
    Gloucester, UK
    Posts
    78

    Re: Doubt about treeview and nodes

    Use the node.tag to store your referenced object.

    For example the following code has a form with a command button, a treeview and a class.

    'Form Code
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5. Dim MyClass As New Class1
    6.  
    7. MyClass.User = "Test"
    8.  
    9. Dim Node As Node
    10.  
    11. Set Node = TreeView1.Nodes.Add(, , , MyClass.User)
    12. Set Node.Tag = MyClass
    13.  
    14. End Sub
    15.  
    16. Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    17.  
    18. Dim MyClass As Class1
    19.  
    20. Set MyClass = Node.Tag
    21. Call MsgBox(MyClass.User)
    22.  
    23. End Sub
    24.  
    25.  
    26. 'Class1 code
    27.  
    28. Option Explicit
    29. Private m_User As String
    30.  
    31. Public Property Get User() As String
    32.  
    33. User = m_User
    34.  
    35. End Property
    36.  
    37. Public Property Let User(Name As String)
    38.  
    39. m_User = Name
    40.  
    41. End Property

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Doubt about treeview and nodes

    Thanks, I never used the VB6 Treeview control until now. Also, I thought the tag property only worked with strings.

  4. #4
    Lively Member
    Join Date
    Jan 1999
    Location
    Gloucester, UK
    Posts
    78

    Re: [RESOLVED] Doubt about treeview and nodes

    Oh forgot to mention. If you declare a variable as an object, you can then use TypeOf to compare it to a specific type, for example:

    VB Code:
    1. Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    2.  
    3. dim MyObj as object
    4. Dim MyClass As Class1
    5.  
    6. Set MyObj = Node.Tag
    7.  
    8. If typeof Myobj is class1 then
    9.  set MyClass=MyObj
    10. Call MsgBox(MyClass.User)
    11. end if
    12.  
    13. 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