Treeview Searches...

Does anyone know how to search a treeview to search for portion of text on a node? And, if the text occurs more than once, how would I 'continue' the search?

Thanks in advance.

Comments

  • Does "find/replace" work?

    BattleGuard

    : Does anyone know how to search a treeview to search for portion of text on a node? And, if the text occurs more than once, how would I 'continue' the search?
    :
    : Thanks in advance.
    :

  • Hi,

    I'm pasting a suggestion that takes user input and searches a treeview from top to bottom including child nodes, grandchild nodes, etc.
    It involves using Regular Expressions. I've tried the following code.

    [code]
    Imports System.Text.RegularExpressions
    [/code]

    and also

    [code]
    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
    'Only Search for Matches if something is entered
    If txt.Text <> String.Empty Then
    Find()
    End If
    End Sub
    Private Sub Find()
    Dim oNode As TreeNode
    'For each top level node
    For Each oNode In tv.Nodes
    If MatchNode(oNode) Then
    'On First Match Exit
    Exit Sub
    End If
    Next
    'If you get to here, no match has been found
    MessageBox.Show("No Match")
    End Sub
    Private Function MatchNode(ByVal oNode As TreeNode) As Boolean
    Dim oRegex As Regex
    Dim oChild As TreeNode
    'Match Any Characters
    'Followed by what has been type in txt
    'Followed by any characters
    If oRegex.IsMatch(oNode.Text, ".*" & txt.Text & ".*", RegexOptions.IgnoreCase) Then
    tv.SelectedNode = oNode
    tv.Select() 'Select the Node that matches
    Return True
    End If
    'Check child nodes
    For Each oChild In oNode.Nodes
    If MatchNode(oChild) Then
    Return True
    End If
    Next
    End Function
    [/code]

    Right now it finds the first match with no Find Next options. I'm sure you could find a way to keep track of where the first match occurs (using the Index of the oNode variable perhaps) and then have a FindNext method that accomplishes the same task but from a different starting point.

    Hope that helps,
    Cheers,

    Chris

    : Does anyone know how to search a treeview to search for portion of text on a node? And, if the text occurs more than once, how would I 'continue' the search?
    :
    : Thanks in advance.
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories