我不知何故被卡住了,无法看到森林里的树木。
我想做什么: 我有一大堆数据(大约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<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类?但我不想使用与之关联的TreeView。有this示例,但它在C中,我似乎无法在VBNet中应用它(在实现ITreeNode时遇到困难)。
我的问题: 我怎样才能使用TreeView的功能,例如“treeView1.Nodes.Add(topNode)”或“treeView1.Nodes(0).Nodes.Find(searchterm,True)”而不在我的表单上实际使用它(我只是需要它)构建我的数据,而不进行可视化。)
我希望这是有道理的,任何人都可以指出我正确的方向!
答案 0 :(得分:1)
虽然TreeNode
位于System.Windows.Forms
命名空间中,但似乎没有任何内容真正与WinForms绑定(它似乎已经在其他几个命名空间中继承),所以,假设它为您提供了所需的功能,您不能只使用它吗? e.g。
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