VB.Net - 如何动态搜索所有TreeView节点中的字符串,扩展和折叠匹配(或不匹配)搜索字符串的节点?

时间:2015-06-14 03:56:59

标签: vb.net search treeview collapse expand

我正在尝试在树视图组件上实现动态搜索,我几乎完成了它,除了因为它是基于文本框的textchanged事件的动态搜索,搜索字符串的第一个字符是总是找到,所以搜索功能会扩展所有节点,因为它们是有效的匹配。

问题在于,当搜索字符串变得更加完整时,那些在匹配时展开的节点现在需要折叠,因为它们不再匹配搜索字符串...而且这种情况不会发生......当搜索字符串改变时,我找不到折叠和扩展节点的方法......

我已经上传了一个视频和Visual Studio 2012解决方案,因此您可以查看它并查看我丢球的位置......

这是我的函数代码进行搜索:(你可以在视频中看到它按预期工作,所以我的问题是节点在匹配(或不匹配)搜索字符串时的扩展/折叠。 / p>

我在“FindRecursive”函数中实现了一些想法来折叠和扩展节点,但它没有按预期工作。由于我的逻辑错误,我甚至设法将控件置于无限循环中。

非常感谢任何帮助,

韩国社交协会!

Video showing the problem

Visual Studio 2012 Project + Test File

Private Sub txtFiltroIDs_TextChanged(sender As Object, e As EventArgs) Handles txtFilterToolIDs.TextChanged
        ClearBackColor()
        FindByText()
    End Sub

    Private Sub FindByText()
        Dim nodes As TreeNodeCollection = tviewToolIDs.Nodes
        Dim n As TreeNode
        For Each n In nodes
            FindRecursive(n)
        Next
    End Sub

    Private Sub FindRecursive(ByVal tNode As TreeNode)
        If txtFilterToolIDs.Text = "" Then
            tviewToolIDs.CollapseAll()
            tviewToolIDs.BackColor = Color.White
            ExpandToLevel(tviewToolIDs.Nodes, 1)
        Else
            Dim tn As TreeNode
            For Each tn In tNode.Nodes
                ' if the text properties match, color the item
                If tn.Text.Contains(txtFilterToolIDs.Text) Then
                    tn.BackColor = Color.Yellow
                    tn.EnsureVisible()        'Scroll the control to the item
                End If

                FindRecursive(tn)
            Next
        End If
    End Sub

    Private Sub ClearBackColor()
        Dim nodes As TreeNodeCollection
        nodes = tviewToolIDs.Nodes
        Dim n As TreeNode
        For Each n In nodes
            ClearRecursive(n)
        Next
    End Sub

    Private Sub ClearRecursive(ByVal treeNode As TreeNode)
        Dim tn As TreeNode
        For Each tn In treeNode.Nodes
            tn.BackColor = Color.White
            ClearRecursive(tn)
        Next
    End Sub

1 个答案:

答案 0 :(得分:1)

按照我的初步评论,尝试以下内容:

Private Sub txtFiltroIDs_TextChanged(sender As Object, e As EventArgs) Handles txtFilterToolIDs.TextChanged
    tviewToolIDs.BeginUpdate()
    tviewToolIDs.CollapseAll()
    ClearBackColor()
    FindByText()
    tviewToolIDs.EndUpdate()
    tviewToolIDs.Refresh()
End Sub
相关问题