Iki
Iki

Reputation: 119

How to search multiple treeview nodes at a particular depth

Please does anyone know how I can search the text of multiple treeview nodes at a particular depth on the click of a button? The treeview nodes are arranged as follows:

enter image description here

I want to prevent the user from entering duplicate grandchild nodes of the same title into the treeview, i.e entering 'Movie 2' a second time should throw up a message that Movie 2 has already been entered; if not, then add the new movie title.

The grandchild node titles are fed into the treeview from a textbox. I am using Visual Basic 2010 Express. Thank you in advance.

The code I am using is:

Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
        

'New movie title has been introduced into the study
        Dim SelectedNode As TreeNode
        SelectedNode = TreeView1.SelectedNode

        'To avoid entering duplicate movies title
        Dim NewMovieName As String = TextBox1.Text.Trim.ToLower ' The content of that node
        Dim parentNode = SelectedNode.Parent ' Get the parent
        Dim childNodes As TreeNodeCollection = parentNode.Nodes ' Get all the children 
        Dim WeHaveDuplicate As Boolean = False ' We use this to flag if a duplicate is found.  Initially set to false.

        For Each tempNode As TreeNode In childNodes
            'Test that we have the same name but not referring to the same node
            If tempNode.Text.Trim.ToLower = NewMovieName And tempNode IsNot parentNode Then WeHaveDuplicate = True
        Next

        If WeHaveDuplicate = True Then
            'Send message to user
            MsgBox(TextBox1.Text & " as a parameter has already been considered.", vbOKOnly)
            Exit Sub
        Else
            parentNode.Nodes.Add(TextBox1.Text)
            TreeView1.ExpandAll()
        End If
        Exit Sub

    End Sub

All help would be greatly appreciated. Thank you.

Upvotes: 0

Views: 1719

Answers (1)

JStevens
JStevens

Reputation: 2152

Here is a little snippet that I use frequently. It will find a node by it's text. It will also highlight and expand the node found.

Notice it is recursive, so it will search to the bottom of the supplied node collection (param). If this supplied collection is the root node, then it will search the whole tree.

I usually apply a unique string to the node.tag property. If you adjust the function to look for that, you can have duplicate text displaying while still having a unique string to look for...

''' <summary>
''' Find and Expand Node in Tree View
''' </summary>
Private Function FindNode(ByVal SearchText As String, ByVal NodesToSearch As TreeNodeCollection, ByVal TreeToSearch As TreeView) As TreeNode
    Dim ReturnNode As TreeNode = Nothing
    Try
        For Each Node As TreeNode In NodesToSearch
            If String.Compare(Node.Text, SearchText, True) = 0 Then
                TreeToSearch.SelectedNode = Node
                Node.Expand()
                ReturnNode = Node
                Exit For
            End If
            If ReturnNode Is Nothing Then ReturnNode = FindNode(SearchText, Node.Nodes, TreeToSearch)
        Next
    Catch ex As Exception
        Throw
    End Try
    Return ReturnNode
End Function

Edited:
Per your recent comment,
You might try using it like this...

WeHaveDuplicate = (FindNode("Movie 2", TreeView1.Nodes, TreeView1) Is Nothing)
If WeHaveDuplicate = True Then
  'message user of dupe
Else
  'add movie
End If

Upvotes: 1

Related Questions