Treeview and regex
-
I'm new to vb.net and I was wondering if it would be possible to use regex on a treeview? I haven't got any code but at least that makes for a quick read.. what I would like is to be able to traverse a treeview for a regex, then building a new treeview with the nodes found maintaining the original structure of the tree (parent - child) inputs are extremely welcome peace How terrible is wisdom when it brings no profit to the wise
-
I'm new to vb.net and I was wondering if it would be possible to use regex on a treeview? I haven't got any code but at least that makes for a quick read.. what I would like is to be able to traverse a treeview for a regex, then building a new treeview with the nodes found maintaining the original structure of the tree (parent - child) inputs are extremely welcome peace How terrible is wisdom when it brings no profit to the wise
In a nutshell ... Private Sub btnMatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMatch.Click targetTree.Nodes.Clear() Dim Expression As New Regex(txtExpression.Text) Dim Matches As MatchCollection For Each node As TreeNode In sourceTree.Nodes If Expression.IsMatch(node.FullPath) Then targetTree.Nodes.Add(node.Clone) End If CheckNodes(Expression, node, targetTree) Next End Sub Private Sub CheckNodes(ByVal expression As Regex, ByVal node As TreeNode, ByVal target As TreeView) For Each child As TreeNode In node.Nodes If expression.IsMatch(child.FullPath) Then NodeParent(target, node).Nodes.Add(child.Clone) End If CheckNodes(expression, child, target) Next End Sub Private Function NodeParent(ByVal target As TreeView, ByVal node As TreeNode) As TreeNode If node.Parent Is Nothing Then For Each Parent As TreeNode In target.Nodes If Parent.Text = node.Text Then Return Parent Next Return target.Nodes.Add(node.Text) End If Return NodeParent(target, node.Parent).Nodes.Add(node.Text) End Function