VB.Net - 如何在展开和折叠匹配(或不匹配)搜索字符串的节点的所有 TreeView 节点中动态搜索字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30825796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.Net - How to dynamicaly search for a string in all TreeView nodes expanding and collapsing nodes matching (or not) the search string?
提问by Daniel Santos
I′m trying to implement dynamic search on a treeview component, and I′m almost done with it, except that since it′s a dynamic search based on the textchanged event of a textbox, the first characters of the search string are always found, so the search function expand all nodes because they are a valid match.
我正在尝试在树视图组件上实现动态搜索,我几乎完成了它,除了因为它是基于文本框的 textchanged 事件的动态搜索,所以总是找到搜索字符串的第一个字符,因此搜索功能会扩展所有节点,因为它们是有效匹配项。
The thing is that as the search string becomes more complete, those nodes that were expanded when they had a match, now needs to be collapsed because they no longer match the search string... and this is not happening... I could not find yet a way to collapse and expand the nodes dinamically as the search string changes...
问题是,随着搜索字符串变得更加完整,那些在匹配时展开的节点现在需要折叠,因为它们不再匹配搜索字符串……而这没有发生……我不能找到一种随着搜索字符串更改而动态折叠和扩展节点的方法......
I have uploaded a video and the Visual Studio 2012 solution so you can take a look at it and see where I′m dropping the ball...
我上传了一个视频和 Visual Studio 2012 解决方案,所以你可以看看它,看看我在哪里丢球......
This is the code of my function that does the search: (You can see in the video it works as expected, so my problem is the expanding/collapsing of the nodes as they match(or not) the search string.
这是我执行搜索的函数的代码:(您可以在视频中看到它按预期工作,所以我的问题是节点在匹配(或不匹配)搜索字符串时展开/折叠。
I′ve implemented some ideas in the "FindRecursive" function to collapse and expand the nodes, but it′s not working as expected. I managed to even put the control in an infinite loop due to my wrong logic.
我已经在“FindRecursive”函数中实现了一些想法来折叠和扩展节点,但它没有按预期工作。由于我的错误逻辑,我什至设法将控件置于无限循环中。
Any help will be very appreciated,
任何帮助将不胜感激,
Tks!
太棒了!
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
采纳答案by Idle_Mind
Following my initial comments, try something like:
按照我最初的评论,尝试以下操作:
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

