软糖来回答罗。
公司主营业务:成都网站建设、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出集贤免费做网站回馈大家。
treenode只有text和name两个string属性。
所以给你写的函数是返回string数组,如需要可自行转换类型Integer
Private Function GetParents(ByVal node As TreeNode) As String()
Dim list As New List(Of String)
Do Until node.Parent Is Nothing
node = node.Parent
list.Add(node.Text)
Loop
Return list.ToArray()
End Function
下面是Listbox1的测试代码,把父节点都添加到Listbox1(在Click后)
Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
Dim k = GetParents(TreeView1.Nodes(0).Nodes(0).Nodes(0).Nodes(0))
ListBox1.Items.AddRange(k)
End Sub
Public Class Form1
Dim node(5) As TreeNode
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim root As TreeNode
With TreeView1
.Nodes.Clear()
.ShowLines = True
.ShowPlusMinus = True
.ShowRootLines = True
root = .Nodes.Add("仓库") '增加根节点
.SelectedNode = root '在此根节点下添加子节点
For i = 1 To 6
node(i - 1) = .SelectedNode.Nodes.Add(i.ToString "号仓库")
Next
.ExpandAll()
End With
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) = 100 And Val(TextBox1.Text) = 699 Then
node(Val(TextBox1.Text) \ 100 - 1).Nodes.Add(TextBox1.Text)
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If Val(TextBox2.Text) = 1000000 And Val(TextBox2.Text) = 6999999 Then
For Each child As TreeNode In node(Val(TextBox2.Text) \ 1000000 - 1).Nodes
If child.Text = TextBox2.Text.Substring(1, 3) Then
child.Nodes.Add(TextBox2.Text)
Exit For
End If
Next
End If
End Sub
End Class
这种不要用递归,知道关键字长度,知道节数用判断就可以了,取前3位第一节,第二节取3个为父节点,取全部为第二节关键字,第三节取前6个为父节点,取全部为第三节关键字.
并没有要求培养字段,查询时从len(ID)=3*N,每次循环时N都+1
select * from tablename where len(id)=3
select case N
case 1
treeview.node.add id,name
case 2
treeview.node(left(id,3)).node.add id,name
case 3
treeview.node(left(id,3)).node(left(id,6)).add id,name
end select
查询结果排个升序就不会存在这样的问题了,你刚才说的那种,如果中间没有比001001001001001更短的关键字,那么就脱节,这种是不可能添加到treeview中.
递归一般用在不知道节数,没有关键字的情况,比如系统目录结构.
递归整个C盘目录:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iDir As IO.Directory
Dim node As New TreeNode
'先把C盘添加到树中
TreeView1.Nodes.Clear()
node.ImageIndex = 0
node.Text = "本地磁盘 C:"
node.SelectedImageIndex = -1
TreeView1.Nodes.Add(node)
Dim i As Integer
'获取C:盘根目录下的文件夹
Dim str() As String = IO.Directory.GetDirectories("D:\")
For i = 0 To str.GetUpperBound(0)
'调用遍历过程
AddDirectory("C:", str(i), node)
Next
node = Nothing
iDir = Nothing
End Sub
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim iDir As IO.Directory
'Dim iDirInfo As IO.DirectoryInfo
Dim i As Integer
Dim node As New TreeNode
'先添加本目录,从文件夹路径分析出文件夹名称
node.Text = Strings.Replace(strPath, strFatherPath "\", "", , 1)
'为单个节点指定节点未被选中时显示的图标
node.ImageIndex = 1
'为单个节点指定节点被选中时显示的图标
node.SelectedImageIndex = 2
nodeFather.Nodes.Add(node)
Application.DoEvents()
Try
Dim str() As String = IO.Directory.GetDirectories(strPath)
'遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), node)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
node = Nothing
iDir = Nothing
End Sub
用数据集操作更加直接。
具体来说,添加节点时指定节点KEY=ID(如果是数字ID前面加一个任意字母),并且把节点附加到父节点上(按上述规则添加很容易找到父节点),没有父节点则作为根节点。
应该能够看出添加时是按层级添加的,即先添加父节点后子节点,数据集可以先按层级排序。