[Help]Treeview Search Button[Solved]

Posts 17 of 7 · Page 1 of 1
[Help]Treeview Search Button[Solved]
How to I make a search button with a textbox for a TreeView?

Ex: I write "Age Of Empires" in the textbox and when I click SEARCH, i want that it selects the root or the child that has Age of Empires in the named.

Thanks
First add this function to your form:

Code:
Private Sub FindNodes(ByVal n As TreeNode)
        Dim x As TreeNode
        For Each x In n.Nodes
            If x.Text = Me.TextBox1.Text Then
                x.BackColor = Color.Indigo
                x.Parent.ExpandAll()
            End If
            FindNodes(x)
        Next
    End Sub
Then double click textbox1 and add the following code to it (Textbox_TextChanged Event):

Code:
Dim nodes As TreeNodeCollection = TreeView2.Nodes
        Dim n As TreeNode
        For Each n In nodes
            FindNodes(n)
        Next
Will expand and highlight all roots and children containing the specified words !!
It doesnt work , when i click search , nothing happens?

*EDIT* it works but only when i type a cd key.
ex: i want that when i type Age of Mythology and I click search, it selects the text because now it only select the child (cd-key) and not the root (games)
Quote Originally Posted by ModelCookie2 View Post
It doesnt work , when i click search , nothing happens?

*EDIT* it works but only when i type a cd key.
ex: i want that when i type Age of Mythology and I click search, it selects the text because now it only select the child and not the root
OK Got It. Here's the final edited code:

Code:
Private Sub FindNodes(ByVal n As TreeNode)
        Dim x As TreeNode
        For Each x In n.Nodes
            x.BackColor = Color.White
        Next
        For Each x In n.Nodes
            If UCase(x.Text) = UCase(Me.TextBox1.Text) Then
                x.BackColor = Color.LightGray
                x.Parent.BackColor = Color.LightGray
                x.Parent.ExpandAll()
            End If
            FindNodes(x)
        Next
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim nodes As TreeNodeCollection = TreeView2.Nodes
        Dim x As TreeNode
        For Each x In nodes
            x.BackColor = Color.White
        Next
        Dim n As TreeNode
        For Each n In nodes
            If UCase(n.Text) = UCase(TextBox1.Text) Then
                n.BackColor = Color.LightGray
            End If
            FindNodes(n)
        Next
    End Sub
Tested and working. Enjoy
It works?
Quote Originally Posted by Lolland View Post
It works?
If he said so, so yes !!
Mark it solved already xD
Posts 17 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?