Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to have Tree Nodes containing some customized Properties to store some data into it is it possible please explain with vb.net code for windows form.

Thanks and regards
Posted

You will need to create a custom treeview that inherits from TreeView.
You will also need to create a custom treeview node class that inherits from TreeNode.

VB
Public Class TreeViewTest
    Inherits TreeView

    Public Overloads Property Nodes() As List(Of TreeViewNode)

End Class

Public Class TreeViewNode
    Inherits TreeNode
    Private m_iMyCustomNodeProperty As Integer

    Public Property MyCustomNodeProperty As Integer
        Get
            Return m_iMyCustomNodeProperty
        End Get
        Set(value As Integer)
            m_iMyCustomNodeProperty = value
        End Set
    End Property
End Class


Once your custom treeview class is complete you can use it in this manner.
VB
Dim oMyCustomTree As New TreeViewTest
oMyCustomTree.Nodes(0).MyCustomNodeProperty = 1
 
Share this answer
 
Comments
Madhukumar N 20-Aug-14 1:45am    
Thanks for the help but after doing the above i couldn't be able to add child nodes into that node can you help?

The code is
Dim DT As New DataTable()
Dim DT1 As New DataTable()
Dim filter As String = ""
Dim OrderBy As String = ""

Try
DT = DS.Tables(0)
filter = "Branchparent is null"
OrderBy = "TreeOrderInd ASC"
For Each row In DS.Tables(0).Select(filter, OrderBy)
''tl.Nodes.Add(row.Item("BranchID").ToString(), row.Item("BranchName").ToString())
Dim Parenttreenode As New CustomTreeNode()
'Parenttreenode.Nodes.Add(row.Item("BranchID").ToString(), row.Item("BranchName").ToString())
Parenttreenode = tl.Nodes.Add(row.Item("BranchID").ToString(), row.Item("BranchName").ToString())
Parenttreenode.ImageIndex = 0
Parenttreenode.BranchType = ReplacevalueifNull(row.Item("BranchType").ToString(), 0)
Parenttreenode.BranchSubType = ReplacevalueifNull(row.Item("BranchSubType").ToString(), 0)
Parenttreenode.TreeOrderInd = ReplacevalueifNull(row.Item("TreeOrderInd").ToString(), 0)
''1st Tier Node
Dim filter1 = "BranchParent='" & row.Item("BranchID").ToString() & "'"
For Each row1 In DS.Tables(0).Select(filter1, OrderBy)
Dim Childtreenode1 As New CustomTreeNode()
Parenttreenode.Nodes.Add(row1.Item("BranchID").ToString(), row1.Item("BranchName").ToString())
'Childtreenode1 = Parenttreenode.Nodes.Add(row1.Item("BranchID").ToString(), row1.Item("BranchName").ToString())
Childtreenode1.ImageIndex = 1
Childtreenode1.BranchType = ReplacevalueifNull(row.Item("BranchType").ToString(), 0)
Childtreenode1.BranchSubType = ReplacevalueifNull(row.Item("BranchSubType").ToString(), 0)
Childtreenode1.TreeOrderInd = ReplacevalueifNull(row.Item("TreeOrderInd").ToString(), 0)
''2nd Tier Node
Dim filter2 = "BranchParent='" & row1.Item("BranchID").ToString() & "'"
For Each row2 In DS.Tables(0).Select(filter2, OrderBy)
Dim Childtreenode2 As New CustomTreeNode()
Childtreenode2 = Childtreenode1.Nodes.Add(row2.Item("BranchID").ToString(), row2.Item("BranchName").ToString())
Childtreenode2.ImageIndex = 2
Childtreenode2.BranchType = ReplacevalueifNull(row.Item("BranchType").ToString(), 0)
Childtreenode2.BranchSubType = ReplacevalueifNull(row.Item("BranchSubType").ToString(), 0)
Childtreenode2.TreeOrderInd = ReplacevalueifNull(row.Item("TreeOrderInd").ToString(), 0)
''3rd Tier Node
Dim filter3 = "BranchParent='" & row2.Item("BranchID").ToString() & "'"
For Each row3 In DS.Tables(0).Select(filter3, OrderBy)
Dim Childtreenode3 As New CustomTreeNode()
Childtreenode3 = Childtreenode2.Nodes.Add(row3.Item("BranchID").ToString(), row3.Item("BranchName").ToString())
Childtreenode3.ImageIndex = 3
Childtreenode3.BranchType = ReplacevalueifNull(row.Item("BranchType").ToString(), 0)
Childtreenode3.BranchSubType = ReplacevalueifNull(row.Item("BranchSubType").ToString(), 0)
Childtreenode3.TreeOrderInd = ReplacevalueifNull(row.Item("TreeOrderInd").ToString(), 0)
''4th Tier Node
Dim filter4 = "BranchParent='" & row3.Item("BranchID").ToString() & "'"
For Each row4 In DS.Tables(0).Select(filter4, OrderBy)
Dim Childtreenode4 As New CustomTreeNode()
Childtreenode4 = Childtreenode3.Nodes.Add(row4.Item("BranchID").ToString(), row4.Item("BranchName").ToString())
Childtreenode4.ImageIndex = 4
Childtreenode4.BranchType
Yea.

Node.Attributes.Add("SomeDate", "11")
 
Share this answer
 
Comments
Madhukumar N 12-Aug-14 5:23am    
It must be like a property of a all the Nodes the values will change for nodes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900