Results 1 to 7 of 7

Thread: [RESOLVED] Treenode search

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] Treenode search

    I know that I can use treeview.nodes.find() but that will find the exact text so how would I search a treeview to find like items?

    I have come close but this only searches the parents, never the children. Would I have to do a double search, start at parent and then get all nodes in parent?

    Code:
      Private Function SearchTree(ByVal sSearchString As String) As Boolean
            Dim l As Integer
            Dim n As TreeNode
            'Dim sSearchString As String
    
    
            For Each n In FrmMain.tvwMain.Nodes("RecipeBank").Nodes
                'MsgBox(n.Text)
                If CBool(InStr(1, n.Text, sSearchString, vbTextCompare)) Then
                    FrmMain.tvwMain.SelectedNode = n
                    'MsgBox(FrmMain.tvwMain.SelectedNode)
                    SearchTree = True
                    lLastNodeFound = l
                    'or call the click event : TreeView1_NodeClick n
                    Exit Function
                End If
            Next
            SearchTree = False
            lLastNodeFound = 0
        End Function

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Treenode search

    To search the child's, you need to make some kind of recursive function...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Treenode search

    try this. it's a recursive search:

    vb Code:
    1. Private Sub SearchTree(ByVal startNode As TreeNode, ByVal sSearchString As String)
    2.     If startNode.Text.Contains(sSearchString) Then
    3.         startNode.ForeColor = Color.Red
    4.     Else
    5.         startNode.ForeColor = Color.Black
    6.     End If
    7.     For Each n As TreeNode In startNode.Nodes
    8.         SearchTree(n, sSearchString)
    9.     Next
    10. End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Treenode search

    I thought it might be a recursive function. Was hoping there was another way.

    So, doing it that way, it is possible to pause it after each one found and then have a button to continue? until it reaches end?

    So search for a string, once it finds one stop search and highlight the node, instead of turning it a different color, but then have a button to continue the search from that spot on. I know I have to remember where it left off which shouldn't be a problem. but pausing it is the problem. how do you pause a recursive search lol

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Treenode search

    use an okcancel msgbox in the function

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Treenode search

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private cancelled As Boolean = False
    4.  
    5.     Private Sub SearchTree(ByVal startNode As TreeNode, ByVal sSearchString As String)
    6.         If cancelled Then Return
    7.         Dim response As MsgBoxResult
    8.         If startNode.Text.Contains(sSearchString) Then
    9.             startNode.TreeView.SelectedNode.BackColor = startNode.TreeView.BackColor
    10.             startNode.TreeView.SelectedNode.ForeColor = startNode.TreeView.ForeColor
    11.             startNode.TreeView.SelectedNode = startNode
    12.             startNode.BackColor = SystemColors.Highlight
    13.             startNode.ForeColor = SystemColors.HighlightText
    14.             response = MsgBox("click ok to continue search, or cancel to abort search", MsgBoxStyle.OkCancel Or MsgBoxStyle.DefaultButton1 Or MsgBoxStyle.Question)
    15.             If response = MsgBoxResult.Cancel Then cancelled = True
    16.         End If
    17.         For Each n As TreeNode In startNode.Nodes
    18.             SearchTree(n, sSearchString)
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    23.         SearchTree(TreeView1.Nodes(0), "searchText")
    24.         cancelled = False
    25.         TreeView1.SelectedNode.BackColor = TreeView1.BackColor
    26.         TreeView1.SelectedNode.ForeColor = TreeView1.ForeColor
    27.         TreeView1.Focus()
    28.     End Sub
    29.  
    30. End Class
    Last edited by .paul.; Feb 27th, 2011 at 02:31 PM.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Treenode search

    Thank you once again paul, that worked as expected.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width