vb net中树视图节点最后一级的子项如何统计和获取?
How to count and get child items of the last level of tree view node in vb net?
如果像下图这样只有在根元素上,我想统计并获取树视图的子名称。我在Item1位置。
Item1
|_____SubItem1
|___A
|___B
结果应该是Count:2
,项目:A, B
此解决方案依赖于您的 TreeView 深度不变。如果您要拥有更深的层次,则需要对其进行智能化处理。
For Each nd As TreeNode In TestTreeView.Nodes
If nd.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndChild As TreeNode In nd.Nodes
If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them
Dim outputText As String = String.Concat(ndChild.Text, " ",
ndChild.Nodes.Count)
For Each ndSubChild As TreeNode In ndChild.Nodes
outputText = String.Concat(outputText, " ", ndSubChild.Text)
Next
Debug.Print(outputText)
End If
Next
End If
Next
还有一个可能的解决方案,使用 LINQ:
Dim result = (From node as TreeNode in TreeView1.TopNode.Nodes
Select New With {
.Count = node.Nodes.Count,
.Items = String.Join(",", node.Nodes.Cast(Of TreeNode).AsEnumerable().Select(Function(childNode) childNode.Name).ToArray())
})
**'Its work for me. Thanks**
Dim outputText As String
For Each nd As TreeNode In TreeView1.Nodes
If nd.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndChild As TreeNode In nd.Nodes
outputText = String.Concat(ndChild.Text, " - (Total : ", ndChild.Nodes.Count, ")")
TextBox4.Text += outputText & vbNewLine
If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndSubChild As TreeNode In ndChild.Nodes
outputText = String.Concat(vbTab, " - ", ndSubChild.Text)
TextBox4.Text += outputText & vbNewLine
If ndSubChild.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndSubSubChild As TreeNode In ndSubChild.Nodes
outputText = String.Concat(vbTab, vbTab, " - ", ndSubSubChild.Text)
TextBox4.Text += outputText & vbNewLine
Next
End If
Next
End If
Next
End If
Next
如果像下图这样只有在根元素上,我想统计并获取树视图的子名称。我在Item1位置。
Item1
|_____SubItem1
|___A
|___B
结果应该是Count:2
,项目:A, B
此解决方案依赖于您的 TreeView 深度不变。如果您要拥有更深的层次,则需要对其进行智能化处理。
For Each nd As TreeNode In TestTreeView.Nodes
If nd.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndChild As TreeNode In nd.Nodes
If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them
Dim outputText As String = String.Concat(ndChild.Text, " ",
ndChild.Nodes.Count)
For Each ndSubChild As TreeNode In ndChild.Nodes
outputText = String.Concat(outputText, " ", ndSubChild.Text)
Next
Debug.Print(outputText)
End If
Next
End If
Next
还有一个可能的解决方案,使用 LINQ:
Dim result = (From node as TreeNode in TreeView1.TopNode.Nodes
Select New With {
.Count = node.Nodes.Count,
.Items = String.Join(",", node.Nodes.Cast(Of TreeNode).AsEnumerable().Select(Function(childNode) childNode.Name).ToArray())
})
**'Its work for me. Thanks**
Dim outputText As String
For Each nd As TreeNode In TreeView1.Nodes
If nd.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndChild As TreeNode In nd.Nodes
outputText = String.Concat(ndChild.Text, " - (Total : ", ndChild.Nodes.Count, ")")
TextBox4.Text += outputText & vbNewLine
If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndSubChild As TreeNode In ndChild.Nodes
outputText = String.Concat(vbTab, " - ", ndSubChild.Text)
TextBox4.Text += outputText & vbNewLine
If ndSubChild.Nodes.Count > 0 Then 'it has children, lets look at them
For Each ndSubSubChild As TreeNode In ndSubChild.Nodes
outputText = String.Concat(vbTab, vbTab, " - ", ndSubSubChild.Text)
TextBox4.Text += outputText & vbNewLine
Next
End If
Next
End If
Next
End If
Next