以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助] 目录树展开与关闭  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=14270)

--  作者:古奇
--  发布时间:2011/11/14 9:42:00
--  [求助] 目录树展开与关闭

当单击节点时,选定的节点展开,同层的其它节点关闭。。

 

Dim nd As WinForm.TreeNode = trv.SelectedNode
Dim id As Integer = nd.index
If e.Node.Level = 0 Then
    Dim cnt As Integer  = trv.Nodes.Count - 1
    For i As Integer = cnt To 0 Step -1
        If trv.Nodes.Count >= 0 Then
            If i=id
                trv.Nodes(i).Expand()
            Else
                trv.Nodes(i).Collapse()
            End If
        End If
    Next

ElseIf e.Node.Level = 1 Then
    Dim cnt As Integer  = trv.Nodes.Count - 1
    For i As Integer = cnt To 0 Step -1
        If trv.Nodes.Count >= 0 Then
            If i=id
                trv.Nodes(i).Nodes(i).Expand()
            Else
                trv.Nodes(i).Nodes(i).Collapse()
            End If
        End If
    Next

End If

 

这个只能做到根节点,往下的就出错了。。。


--  作者:狐狸爸爸
--  发布时间:2011/11/14 9:55:00
--  

用这个比较好:
 
http://www.foxtable.com/help/topics/2302.htm


大概就是这样:
 
Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1")
For Each nd As WinForm.TreeNode In trv.AllNodes
    If nd.Level = e.Node.Level AndAlso nd.FullPath <> e.Node.FullPath then
        trv.Nodes(i).Collapse()
    End If
Next
e.Node.Expand()


--  作者:古奇
--  发布时间:2011/11/14 11:06:00
--  

图片点击可在新窗口打开查看。。。谢狐爸


--  作者:古奇
--  发布时间:2011/11/14 16:44:00
--  

Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1")
If e.Node.Level = 0 Then
    Dim cnt As Integer  = trv.Nodes.Count - 1
    For i As Integer = cnt To 0 Step -1
        For Each nd As WinForm.TreeNode In trv.AllNodes
            If nd.Level = e.Node.Level AndAlso nd.FullPath <> e.Node.FullPath Then
                trv.Nodes(i).CollapseAll()
            Else
                e.Node.Expand()
            End If
        Next
    Next
ElseIf e.Node.Level = 1 Then
    Dim cnt As Integer  = trv.SelectedNode.ParentNode.Nodes.Count - 1
    For i As Integer = cnt To 0 Step -1
        For Each nd As WinForm.TreeNode In trv.AllNodes
            If nd.Level = e.Node.Level AndAlso nd.FullPath <> e.Node.FullPath Then
                trv.Nodes(Value(0)).Nodes(i).CollapseAll()
            Else
                e.Node.Expand()
            End If
        Next
    Next
ElseIf e.Node.Level = 2 Then
    Dim cnt As Integer  = trv.SelectedNode.ParentNode.Nodes.Count - 1
    For i As Integer = cnt To 0 Step -1
        For Each nd As WinForm.TreeNode In trv.AllNodes
            If nd.Level = e.Node.Level AndAlso nd.FullPath <> e.Node.FullPath Then
                trv.Nodes(Value(0)).Nodes(Value(1)).Nodes(i).CollapseAll()
            Else
                e.Node.Expand()
            End If
        Next
    Next
ElseIf e.Node.Level = 3 Then
    Dim cnt As Integer  = trv.SelectedNode.ParentNode.Nodes.Count - 1
    For i As Integer = cnt To 0 Step -1
        For Each nd As WinForm.TreeNode In trv.AllNodes
            If nd.Level = e.Node.Level AndAlso nd.FullPath <> e.Node.FullPath Then
                trv.Nodes(Value(0)).Nodes(Value(1)).Nodes(Value(2)).Nodes(i).CollapseAll()
            Else
                e.Node.Expand()
            End If
        Next
    Next
End If

 

可不可以简化代码   不管目录树的层数。。。?


--  作者:狐狸爸爸
--  发布时间:2011/11/14 16:48:00
--  
先告诉我,你的设计任务是什么?
--  作者:古奇
--  发布时间:2011/11/14 16:52:00
--  
仓库目录树。。有的商品规格分类层比较多,有的层比较少,我想单击是展开,双击就选定。。。
--  作者:狐狸爸爸
--  发布时间:2011/11/14 16:54:00
--  

单击展开,这样不就行了吗:

 

e.Node.Expand()