使用 TreeNode Class 在没有 TreeView 的情况下处理分层结构化数据

Use TreeNode Class to work with hierarchically structured data without a TreeView

我不知何故被困住了,只见树木不见森林。

我想做的事情: 我有一个很大的数据列表(大约 6000 个节点),目前很简单:

目前,这是平面数据,但我想创建它的层次结构,所以我可以

我试过的: 我从这段代码开始: see link

<Serializable> _
Public Class TreeNode
    Private _uniqueID As Integer
    Private _name As String
    Private _parentID As Integer
    Private _depth As Integer
    Private _children As ArrayList
    Public Sub New()
    End Sub
    Public Sub New(name As String, parentID As Integer)
        Me.New(0, name, parentID, -1)
    End Sub
    Public Sub New(uniqueID As Integer, name As String, parentID As Integer, depth As Integer)
        _uniqueID = uniqueID
        _name = name
        _parentID = parentID
        _depth = depth
    End Sub
    ''' <summary>
    ''' Gets or sets the unique ID associated with this category
    ''' </summary>
    ''' <remarks>Once a non-zero ID has been set, it may not be modified.</remarks>
    Public Property UniqueID() As Integer
        Get
            Return _uniqueID
        End Get
        Set
            If _uniqueID = 0 Then
                _uniqueID = value
            Else
                Throw New Exception("The UniqueID property cannot be modified once it has a non-zero value")
            End If
        End Set
    End Property
    Public ReadOnly Property Depth() As Integer
        Get
            Return _depth
        End Get
    End Property
    ''' <summary>
    ''' Gets or sets the label for this node
    ''' </summary>
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set
            _name = value
        End Set
    End Property
    ''' <summary>
    ''' The ID of the parent node
    ''' </summary>
    Public Property ParentID() As Integer
        Get
            Return _parentID
        End Get
        Set
            _parentID = value
        End Set
    End Property
    ''' <summary>
    ''' Gets the children TreeNode objects for this category
    ''' </summary>
    ''' <remarks>In .NET 2.0, this can be modified to use generics, and have type ArrayList&lt;TreeNode></remarks>
    Public Property Children() As ArrayList
        Get
            Return _children
        End Get
        Set
            _children = value
        End Set
    End Property
End Class

我创建了我的树:

Public Dendrogram As List(Of TreeNode)

.. 并向其中添加了所有节点。超级干净,可以理解,但没有功能!

这让我想到了 another approach 但它对于我的目的来说太复杂了。

..然后我想知道:为什么不使用MS的TreeNode Class?但我不想使用与之关联的 TreeView。有 this 示例,但它是在 C 中,我似乎无法在 VBNet 中应用它(卡在 ITreeNode 的实现中)。

我的问题: 我怎样才能使用 TreeView 的功能,例如 "treeView1.Nodes.Add(topNode)" 或 "treeView1.Nodes(0).Nodes.Find(searchterm, True)" 而无需在我的表单上实际使用它(我只需要它来构建我的数据,而不是将其可视化)。

我希望这是有道理的,任何人都可以指出正确的方向!

尽管 TreeNodeSystem.Windows.Forms 命名空间中,但其中似乎没有任何与 WinForms 真正相关的内容(它似乎已经在其他几个命名空间中继承) 那么,假设它为您提供了所需的功能,您就不能直接使用它吗?例如

Imports System.Windows.Forms

Sub Main
    Dim root = New TreeNode("Root")
    root.Nodes.Add("Node 1")
    root.Nodes.Add("Node 2")
    root.Nodes.Add("Node 3")
    root.Nodes(0).Nodes.Add("Node 1.1")
    root.Nodes(0).Nodes(0).Nodes.Add("Node 1.1.1")
    root.Nodes(1).Nodes.Add("Node 2.1")
    PrintNode(root, 0)
End Sub

' Define other methods and classes here
Sub PrintNode(node As TreeNode, level As Integer)
    Console.WriteLine("{0}{1}", New String(" ", level * 2), node.Text)
    For Each child In node.Nodes
        PrintNode(child, level + 1)
    Next
End Sub

输出:

Root
  Node 1
    Node 1.1
      Node 1.1.1
  Node 2
    Node 2.1
  Node 3