VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > VBnet >
  • VB.net遍历搜索Treeview,找到符合字符串条件的节点

ListView具有和Treeview相同的内容,且都有Check属性,带有复选框,现在想让ListView和Treeview的check选择对应起来:
 
        For i = 0 To ListView2.Items.Count - 1
            ListView2.Items(i).Checked = False '先清除所有选择
        Next
        For Each n As TreeNode In GetCheck(TreeView1.Nodes)
            If n.Level = 1 Then  '只处理子节点,不处理根节点
                'Trace.WriteLine(n.Text & "," & n.Level.ToString)
                For i = 0 To ListView2.Items.Count - 1
                    If ListView2.Items(i).SubItems(1).Text = n.Text Then  '与TreeView同步选择
                        ListView2.Items(i).Checked = True
                    End If
                Next
            End If
        Next
 
' 递归搜索
    Private Function GetCheck(ByVal node As TreeNodeCollection) As List(Of TreeNode)
        Dim lN As New List(Of TreeNode)
        For Each n As TreeNode In node
            If n.Checked Then lN.Add(n)
            lN.AddRange(GetCheck(n.Nodes))
        Next
        Return lN
    End Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
反之,想让Treeview和ListView的Check选择对应:
 
    For Each node In TreeView1.Nodes
        NodeAllCheck(node, False)  '先清除所有选择
    Next
    For i As Integer = 0 To ListView2.CheckedItems.Count - 1 '遍历表格
        For Each node In TreeView1.Nodes  '根据表格选择情况,选择对应目录树节点
            CheckNode(node, ListView2.CheckedItems(i).SubItems(1).Text) '递归搜索子节点
        Next
    Next
Private Sub CheckNode(ByVal N As TreeNode, ByVal s As String)
    Dim node As TreeNode
    If N.Text = s Then
        N.Checked = True
    End If
    For Each node In N.Nodes
        CheckNode(node, s)
    Next
End Sub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
全选(或全不选)的方法:
 
        For i As Integer = 0 To ListView2.Items.Count - 1  '表格全选
            ListView2.Items.Item(i).Checked = True
        Next
        For Each node As TreeNode In TreeView1.Nodes  '目录树全选
            NodeAllCheck(node, True)
        Next
Private Sub NodeAllCheck(ByVal N As TreeNode, checkinfo As Boolean)
    Dim node As TreeNode
    N.Checked = checkinfo
    For Each node In N.Nodes
        NodeAllCheck(node, checkinfo)
    Next
End Sub
————————————————
版权声明:本文为CSDN博主「xjnzhidao」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xjnzhidao/article/details/47341387
相关教程