[VB.NET]調整TreeView或TreeNode下的節點
Private Function GetAllNodes(ByVal treeOrNode As Object) As TreeNode()
If Not TypeOf treeOrNode Is TreeNode AndAlso Not TypeOf treeOrNode Is TreeView Then
Throw New ArgumentException("Error param type!!")
End If
Dim nodes As New List(Of TreeNode)
If TypeOf treeOrNode Is TreeNode Then
nodes.Add(treeOrNode)
End If
For Each tn As TreeNode In treeOrNode.Nodes
nodes.AddRange(GetAllNodes(tn))
Next
Return nodes.ToArray
End Function</pre></div>
Public Module TreeViewExtension
#Region “Private Method” Private Function GetAllTreeNodes(ByVal treeOrNode As Object) As TreeNode() If Not TypeOf treeOrNode Is TreeNode AndAlso Not TypeOf treeOrNode Is TreeView Then Throw New ArgumentException(“Error param type!!”) End If
Dim nodes As New List(Of TreeNode)
If TypeOf treeOrNode Is TreeNode Then
nodes.Add(treeOrNode)
End If
For Each tn As TreeNode In treeOrNode.Nodes
nodes.AddRange(GetAllTreeNodes(tn))
Next
Return nodes.ToArray
End Function
Private Sub AdjustAllTreeNodes(ByVal treeOrNode As Object)
Dim allNodes() As TreeNode = GetAllTreeNodes(treeOrNode)
Dim node1, node2 As TreeNode
For idx1 As Integer = allNodes.Count - 1 To 0 Step -1
For idx2 As Integer = idx1 - 1 To 0 Step -1
node1 = allNodes(idx1)
node2 = allNodes(idx2)
If node1.TreeView IsNot Nothing AndAlso node2.TreeView IsNot Nothing AndAlso node1.FullPath = node2.FullPath Then
Dim childNodes(node1.Nodes.Count - 1) As TreeNode
node1.Nodes.CopyTo(childNodes, 0)
node1.Remove()
node2.Nodes.AddRange(childNodes)
End If
Next
Next
End Sub
#End Region
#Region “Public Method” <Extension()> _ Public Sub AdjustAllNodes(ByVal tree As TreeView) AdjustAllTreeNodes(tree) End Sub
<Extension()> _
Public Sub AdjustAllNodes(ByVal node As TreeNode)
AdjustAllTreeNodes(node)
End Sub
#End Region
End Module