|
4#
楼主 |
发表于 2013-11-29 22:06:47
|
只看该作者
本帖最后由 todaynew 于 2013-11-29 22:10 编辑
应该不必这么麻烦。
因为你的tree是确定层级的,每个层级都有特定的含义。这样的话在加载时,对node的key值前面分别加上a、b、c....
这样在单击node事件中,读出node.key,然后用left取出第一个字符(a、b、c....),这样就知道节点所在的层了。
你把这个示例中的SetTree子程序,用以下代码替换一下,然后运行就明白怎么回事了。
Private Sub SetTree(ByVal tree As TreeView, ByVal Parentkey As String)
Dim id As Long, str As String
Dim rs As New ADODB.Recordset
Dim ssql As String
Dim i As Long
Dim node As node
str = Left(Parentkey, 1) '取父节点key的第一个字符
id = Val(Mid(Parentkey, 2)) '取父节点key中的ParentID值
If id = 0 Then
tree.Nodes.Clear
Set node = tree.Nodes.Add(, , Parentkey, "产品目录", "k1", "k2")
node.Expanded = True
End If
ssql = "select * from tblType WHERE ParentID = " & id
rs.Open ssql, CurrentProject.Connection, adOpenStatic, adLockReadOnly
str = Chr(Asc(str) + 1) '字母递增一位
For i = 1 To rs.RecordCount '注意这里获得节点层级
Set node = tree.Nodes.Add(Parentkey, tvwChild, str & rs!id.Value, rs!Name.Value & "--第" & Asc(str) - Asc("a") & "层", "k1", "k2")
Call SetTree(tree, str & rs!id.Value)
rs.MoveNext
Next
rs.Close: Set rs = Nothing
Set node = Nothing
End Sub
|
|