如何搜索TreeView中的所有节点 [英] How to search through all nodes in a TreeView

查看:90
本文介绍了如何搜索TreeView中的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(VB.Net,WinForm)我在填充 TreeView 三个级别方面没有太大成功。我尝试过不同来源的不同代码,但是我试过的那些代码无处可去。



我正在检索我的节点来自数据库。表格如下:

catID | catName | catParent | catImage



对于所有根节点,catParent将不包含任何数据。对于所有其他节点,catParent将包含它所属的父节点。不要将父节点与根节点混淆。



我首先尝试了以下代码(几乎到处都找到,即使在CodeProject上也是如此)。我想使用 .Find 选项,因为它意味着我只需要搜索我的父节点的名称,因为索引不是唯一的。



 私人  Sub  PopulateDocumentCategories( )
' 清除树视图
tvDocuments.Nodes.Clear()
' 从数据库获取所有类别
clReadFromDB.GetDocCategories()
' 通读数据表
For i = 0 clReadFromDB.catData.Rows.Count - 1
' 测试是否分类y有一个父节点
如果 clReadFromDB.catData.Rows(i).Item( 2 )= 然后
' 类别是父节点
tvDocuments.Nodes.Add(< span class =code-keyword>新 System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item( 1 ), _
clReadFromDB.catData.Rows(i).Item( 3 ),_
clReadFromDB.catData.Rows(i).Item(< span class =code-digit> 3 )))
Else
' 条目有一个父节点。需要在TreeView中找到Parent节点,然后添加Child节点
尝试
Dim tn As TreeNode()= tvDocuments.Nodes( 0 )。Nodes.Find (clReadFromDB.catData.Rows(i).Item( 2 )。ToString, True
对于 j 作为 整数 = 0 tn.Length - 1
tvDocuments.SelectedNode = tn(j)
tvDocuments.Nodes.Add( New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i) .Item( 1 ),_
clReadFromDB.catData.Rows(i).Item( 3 ),_
clReadFromDB.catData.Rows(i).Item( 3 )))
下一步
Catch
结束 尝试
下一步 i ' 数据表行
tvDocuments.ExpandAll()
结束 Sub





这只会将根节点添加到TreeView控件并忽略所有其他节点。



返回绘图板并提出以下代码实际上填充了TreeView与根节点(第1层)a第一级子节点(第2层父节点)。但是它不会添加最后一级节点(第3层)。



 私有  Sub  PopulateDocumentCategories()
' 清除Treeview
tvDocuments.Nodes.Clear()
' 全部获取数据库中的类别
clReadFromDB.GetDocCategories()
' 通读datatable
对于 i = 0 clReadFromDB.catData.Rows.Count - 1
' 测试类别是否有父节点
如果 clReadFr omDB.catData.Rows(i).Item( 2 )= 然后
' 类别是根节点
tvDocuments.Nodes.Add( System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows( i).Item( 1 ),_
clReadFromDB.catData.Rows(i).Item( 3 ),_
clReadFromDB.catData.Rows(i).Item( 3 )))
否则
' 条目具有父节点。需要在TreeView中找到Parent节点,然后添加子节点
对于 每个 node 作为 TreeNode tvDocuments.Nodes
' 查找父节点
如果 node.Text = clReadFromDB.catData .Rows(i).Item( 2 然后
' 选择找到的节点
tvDocuments.SelectedNode = node
' 将子节点添加到选定节点
tvDocuments.SelectedNode.Nodes.Add( New Sy stem.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item( 1 ),_
clReadFromDB.catData.Rows(i)。项目( 3 ),_
clReadFromDB.catData.Rows(i).Item( 3 )))
结束 如果
下一步节点' 树视图节点
结束 如果
下一步 i ' 数据表行
tvDocuments.ExpandAll()
结束 Sub





有什么我做错了或不考虑以编程方式填充TreeView?

解决方案

我认为没有必要深入研究 TreeView 人口。只需查看所有添加调用即可。它总是 tvDocuments.Nodes.Add 。如果您只将子节点添加到只有一个对象 tvDocuments 的节点,为什么只有下一级节点会让您感到惊讶?谁将为所有其他节点添加子节点?



无论你做什么,最终都应该使用属性 System.Windows .Forms.TreeNode.Nodes 添加更多节点。你永远不会这样做。请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nodes%28v=vs.110%29.aspx [ ^ ]。



-SA


  填充文档类别 
私有 Sub PopulateDocumentCategories()
' 清除Treeview
tvDocuments.Nodes.Clear()
' 从数据库中获取所有类别
clReadFromDB.GetDocCategories()
' 通读数据表
对于 i = 0 clReadFromDB.catData.Rows.Count - 1
' 测试类别是否有父节点
如果 clReadFromDB.catData.Rows(i).Item( 2 )= 然后
' 类别是根节点
tvDocuments.Nodes.Add( System.Windows.Forms.TreeNode(clReadFromDB) .catData.Rows(i).Item( 1 ),_
clReadFromDB.catData.Ro ws(i).Item( 3 ),_
clReadFromDB.catData.Rows(i).Item( 3 )))
其他

' 条目具有父节点。需要在TreeView中找到Parent节点,然后添加子节点
对于 每个 ParentNode 作为 TreeNode tvDocuments.Nodes
如果 ParentNode.Text.ToString = clReadFromDB.catData.Rows(i).Item( 2 )。ToString 然后
' 选择找到的节点
tvDocuments.SelectedNode = ParentNode
' 将子节点添加到所选节点
tvDocuments.SelectedNode .Nodes.Add( System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item( 1 ), _
clReadFromDB.catData.Rows(i).Item( 3 ),_
clReadFromDB.catData.Rows(i).Item(< span class =code-digit> 3
)))
tvDocuments.Update()

Else
' 查找子节点
对于 每个 ChildNode As TreeNode In ParentNode.Nodes
如果 ChildNode.Text.ToString = clReadFromDB.catData.Rows(i).Item( 2 )。ToString 然后
' 选择找到的节点
tvDocuments.SelectedNode = ChildNode
将子节点添加到选定节点
tvDocuments.SelectedNode.Nodes.Add( System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item( 1 ),_
clReadFromDB.catData.Rows(i).Item( 3 ),_
clReadFromDB.catData.Rows(i).Item( 3 )))
tvDocuments.Update()
结束 如果

下一步 ChildNode

结束 如果

下一步 ParentNode ' Treeview节点
结束 如果
下一步 i ' 数据表行
tvDocuments.ExpandAll()
结束 < span class =code-keyword> Sub





有关此主题的更多详细信息,请参见与递归迭代相关的MSDN链接:

http ://msdn.microsoft.com/en-us/library/wwc698z7(v = VS。 100).aspx [ ^ ]


(VB.Net, WinForm) I am not having much success in populating a TreeView three levels deep. I've tried different code from various sources but those I tried got me nowhere.

I am retrieving my Nodes from a database. The table looks like this:
catID | catName | catParent | catImage

For all Root Nodes the "catParent" will contain no data. For all other Nodes the catParent will contain the Parent Node it belongs to. Do not confuse Parent node with Root node.

I first tried the following code (found almost everywhere, even here on CodeProject). I thought to use the .Find option as it meant that I just had to search for the Name of my Parent Node since Indexes aren't unique.

Private Sub PopulateDocumentCategories()
        'Clear the Treeview
        tvDocuments.Nodes.Clear()
        'Get all the categories from the DB
        clReadFromDB.GetDocCategories()
        'Read through the datatable
        For i = 0 To clReadFromDB.catData.Rows.Count - 1
            'Test to see if category has a Parent Node
            If clReadFromDB.catData.Rows(i).Item(2) = "" Then
                'Category is a Parent Node
                tvDocuments.Nodes.Add(New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item(1), _
                                                                        clReadFromDB.catData.Rows(i).Item(3), _
                                                                        clReadFromDB.catData.Rows(i).Item(3)))
            Else
                'Entry has a Parent node. Need to find the Parent node in the TreeView then add the Child node
                Try
                    Dim tn As TreeNode() = tvDocuments.Nodes(0).Nodes.Find(clReadFromDB.catData.Rows(i).Item(2).ToString, True)
                    For j As Integer = 0 To tn.Length - 1
                        tvDocuments.SelectedNode = tn(j)
                        tvDocuments.Nodes.Add(New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item(1), _
                                                                        clReadFromDB.catData.Rows(i).Item(3), _
                                                                        clReadFromDB.catData.Rows(i).Item(3)))
                    Next
                Catch
                End Try
        Next i 'Datatable rows
        tvDocuments.ExpandAll()
    End Sub



This only adds the Root Nodes to the TreeView control and ignores all other nodes.

Went back to the drawing board and came up with the following code which actually populates the TreeView with the Root Nodes (Tier 1) and first level Child Nodes (Tier 2 Parent Nodes). However it does not add the last level of nodes (Tier 3).

Private Sub PopulateDocumentCategories()
        'Clear the Treeview
        tvDocuments.Nodes.Clear()
        'Get all the categories from the DB
        clReadFromDB.GetDocCategories()
        'Read through the datatable
        For i = 0 To clReadFromDB.catData.Rows.Count - 1
            'Test to see if category has a Parent Node
            If clReadFromDB.catData.Rows(i).Item(2) = "" Then
                'Category is a Root Node
                tvDocuments.Nodes.Add(New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item(1), _
                                                                        clReadFromDB.catData.Rows(i).Item(3), _
                                                                        clReadFromDB.catData.Rows(i).Item(3)))
            Else
                'Entry has a Parent node. Need to find the Parent node in the TreeView then add the Child node
                For Each node As TreeNode In tvDocuments.Nodes
                    'Find the Parent Node
                    If node.Text = clReadFromDB.catData.Rows(i).Item(2) Then
                        'Select the found Node
                        tvDocuments.SelectedNode = node
                        'Add the Child Node to the Selected Node
                        tvDocuments.SelectedNode.Nodes.Add(New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item(1), _
                                                                                             clReadFromDB.catData.Rows(i).Item(3), _
                                                                                             clReadFromDB.catData.Rows(i).Item(3)))
                    End If
                Next node 'Treeview nodes
            End If
        Next i 'Datatable rows
        tvDocuments.ExpandAll()
    End Sub



Is there something that I am doing wrong or not considering while populating the TreeView programmatically ?

解决方案

I see no need in digging into this code deep for the trivial algorithm of the TreeView population. It's enough to look at all your Add calls. It's always tvDocuments.Nodes.Add. If you are adding sub-nodes only to the nodes of only one object tvDocuments, why would having only the next-level nodes surprise you? Who is going to add sub-nodes for all other nodes?

No matter what you do, eventually you should use the property System.Windows.Forms.TreeNode.Nodes to add more nodes. And you never do it. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nodes%28v=vs.110%29.aspx[^].

—SA


'Populate Document Categories
 Private Sub PopulateDocumentCategories()
     'Clear the Treeview
     tvDocuments.Nodes.Clear()
     'Get all the categories from the DB
     clReadFromDB.GetDocCategories()
     'Read through the datatable
     For i = 0 To clReadFromDB.catData.Rows.Count - 1
         'Test to see if category has a Parent Node
         If clReadFromDB.catData.Rows(i).Item(2) = "" Then
             'Category is a Root Node
             tvDocuments.Nodes.Add(New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item(1), _
                                                                     clReadFromDB.catData.Rows(i).Item(3), _
                                                                     clReadFromDB.catData.Rows(i).Item(3)))
         Else

             'Entry has a Parent node. Need to find the Parent node in the TreeView then add the Child node
             For Each ParentNode As TreeNode In tvDocuments.Nodes
                 If ParentNode.Text.ToString = clReadFromDB.catData.Rows(i).Item(2).ToString Then
                     'Select the found Node
                     tvDocuments.SelectedNode = ParentNode
                     'Add the Child Node to the Selected Node
                     tvDocuments.SelectedNode.Nodes.Add(New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item(1), _
                                                                                          clReadFromDB.catData.Rows(i).Item(3), _
                                                                                          clReadFromDB.catData.Rows(i).Item(3)))
                     tvDocuments.Update()

                 Else
                     'Find the Child Node
                     For Each ChildNode As TreeNode In ParentNode.Nodes
                         If ChildNode.Text.ToString = clReadFromDB.catData.Rows(i).Item(2).ToString Then
                             'Select the found Node
                             tvDocuments.SelectedNode = ChildNode
                             'Add the Child Node to the Selected Node
                             tvDocuments.SelectedNode.Nodes.Add(New System.Windows.Forms.TreeNode(clReadFromDB.catData.Rows(i).Item(1), _
                                                                                                  clReadFromDB.catData.Rows(i).Item(3), _
                                                                                                  clReadFromDB.catData.Rows(i).Item(3)))
                             tvDocuments.Update()
                         End If

                     Next ChildNode

                 End If

             Next ParentNode 'Treeview nodes
         End If
     Next i 'Datatable rows
     tvDocuments.ExpandAll()
 End Sub



For more detailed information on the topic here is the MSDN link relating to Recursive Iteration:
http://msdn.microsoft.com/en-us/library/wwc698z7(v=vs.100).aspx[^]


这篇关于如何搜索TreeView中的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆