Hi Rob,
I suggest that you use a List(Of T) to contain objects and build a TreeView
based on the list. Use a Hashtable to maintain the relationship between
each object's key and the corresponding TreeNode, so you can get the
TreeNode corresponding to a given key randomly.
The following is a sample. It requires that you add a TreeView and two
Buttons on the Form.
Public Class Category
Private m_ID As Integer
Private m_Name As String
Private m_Parent As Integer
Public Property ID()
Get
Return m_ID
End Get
Set(ByVal value)
m_ID = value
End Set
End Property
Public Property Name()
Get
Return m_Name
End Get
Set(ByVal value)
m_Name = value
End Set
End Property
Public Property Parent()
Get
Return m_Parent
End Get
Set(ByVal value)
m_Parent = value
End Set
End Property
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal parent
As Integer)
Me.m_ID = id
Me.m_Name = name
Me.m_Parent = parent
End Sub
End Class
Imports System.Collections
Public Class Form1
Private lists As New List(Of Category)
Private hash As New Hashtable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lists.Add(New Category(1, "category 1", 0))
lists.Add(New Category(2, "category 2", 0))
lists.Add(New Category(3, "category 3", 0))
lists.Add(New Category(4, "category 1-1", 1))
lists.Add(New Category(5, "category 1-2", 1))
lists.Add(New Category(6, "category 2-1", 2))
lists.Add(New Category(7, "category 3-1", 3))
BuildTreeView(lists)
End Sub
' build a TreeView according to the passed lists
Public Sub BuildTreeView(ByRef lists As List(Of Category))
Dim children As List(Of Category) = GetChildren(lists, 0)
Dim node As TreeNode
While (children.Count > 0)
If (Me.TreeView1.Nodes.Count = 0) Then
For Each entry As Category In children
node = Me.TreeView1.Nodes.Add(entry.Name)
node.Tag = entry.ID
hash.Add(entry.ID, node)
Next
Else
Dim parentnode As TreeNode = node
For Each entry As Category In children
node = parentnode.Nodes.Add(entry.Name)
node.Tag = entry.ID
hash.Add(entry.ID, node)
Next
End If
children = GetChildren(lists, node.Tag)
While (children.Count = 0)
While (node IsNot Nothing)
If (node.PrevNode IsNot Nothing) Then
node = node.PrevNode
Exit While
ElseIf (node.Parent IsNot Nothing) Then
node = node.Parent
Else
node = Nothing
End If
End While
If (node Is Nothing) Then
Exit Sub
End If
children = GetChildren(lists, node.Tag)
End While
End While
End Sub
' get all children of a specified key
Private Function GetChildren(ByRef lists As List(Of Category), ByVal
parent As Integer) As List(Of Category)
Dim children As New List(Of Category)
For Each entry As Category In lists
If entry.Parent = Parent Then
children.Add(entry)
End If
Next
Return children
End Function
' add a new TreeNode to the TreeView and update the Hashtable.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim node As TreeNode = hash(2)
Dim newnode As TreeNode
If (node IsNot Nothing) Then
newnode = node.Nodes.Add("category 2-2")
newnode.Tag = 8
hash.Add(8, newnode)
End If
End Sub
' remove a TreeNode from the TreeView and update the Hashtable
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.TreeView1.Nodes.Remove(hash(7))
hash.Remove(7)
End Sub
End Class
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.