vb.net操作TreeView控件,生成树节点的实例代码,其中遍历采用递归方法。
-----------------------------------------------
Public Class Form2 Class nn Public name As String Public parent As String Public snumber As String End Class Private Sub Form2_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load Dim nd(10) As nn Dim i As Integer For i = 0 To 8 nd(i) = New nn Next nd(0).name = "root" : nd(0).parent = "0" : nd(0).snumber = "1" nd(1).name = "aaa" : nd(1).parent = "1" : nd(1).snumber = "2" nd(2).name = "bbb" : nd(2).parent = "1" : nd(2).snumber = "3" nd(3).name = "ccc" : nd(3).parent = "2" : nd(3).snumber = "4" nd(4).name = "ddd" : nd(4).parent = "3" : nd(4).snumber = "5" nd(5).name = "eee" : nd(5).parent = "2" : nd(5).snumber = "6" nd(6).name = "fff" : nd(6).parent = "2" : nd(6).snumber = "7" nd(7).name = "ggg" : nd(7).parent = "5" : nd(7).snumber = "8" Me.TreeView1.Nodes.Clear() For i = 0 To 7 addto(nd(i),TreeView1.Nodes) Next End Sub Public Sub addto(ByVal nd As nn,ByVal nodes As TreeNodeCollection) Dim dd As TreeNode Dim tmpd As TreeNode If nd.parent = "0" Then tmpd = nodes.Add(nd.name) tmpd.ToolTipText = nd.snumber Exit Sub Else For Each dd In nodes If nd.parent = dd.ToolTipText Then tmpd = dd.Nodes.Add(nd.name) tmpd.ToolTipText = nd.snumber Exit Sub End If addto(nd,dd.Nodes) Next End If End Sub End Class
代码仅供参考,实际应用可灵活变更。
原文链接:/vb/259135.html