Question Search TreeView Nodes using indexof/contains which is better?

darkcat02

Active member
Joined
Mar 4, 2009
Messages
38
Programming Experience
1-3
hi everyone...

i found this code on this forum.. but im not satisfied on the results.. i tried to recode it but no luck.. i just need your opinions... if you can give example... many thanks...

Function searchTreeview(ByVal SearchString As String, ByVal Nodes As TreeNodeCollection, _
Optional ByVal ExactMatch As Boolean = False, _
Optional ByVal Recursive As Boolean = True) _
As TreeNode

Dim ret As TreeNode
For Each tn As TreeNode In Nodes
If ExactMatch = True Then
If tn.Text = SearchString Then Return tn
Else
If tn.Text.IndexOf(SearchString) <> -1 Then Return tn
End If

If Recursive = True Then
If tn.Nodes.Count > 0 Then
ret = searchTreeview(SearchString, tn.Nodes, ExactMatch, Recursive)
If Not ret Is Nothing Then Return ret
End If
End If
Next
Return Nothing
End Function

Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click
Dim tn As TreeNode = searchTreeview(TextBoxWordFind.Text, TreeViewBible.Nodes, False, True)
If Not tn Is Nothing Then
TreeViewBible.SelectedNode = tn
TreeViewBible.SelectedNode.BackColor = Color.RoyalBlue
TreeViewBible.SelectedNode.ForeColor = Color.White
Else
MsgBox("no match")
End If
End Sub


as i went through this code... im not getting the result i wanted... im confused what indexof really do...

on my treeview (for example only)
Old book
- Book 1 : Title One
- Book 2 : Title Two
- Chapter 1
- Chapter 2
- Paragraph 1
- Chapter 3


here is my scenario...
the color red text are from database... and the black one are only hard coded...
when i search for "Book 1".. no match
but when i search for "1" only... it founds it...

i need to search for the whole "Book 1"...
or just the title itself... anyone could explain this?

especially the person who gave this code... :( thanks a lot
 
case sensitive???

my bad...

does it really need to be that case sensitive... i found out just a seconds ago... when i type for the exact case like the ones in the treeview... it has a match.. but when it was all lower case... no match...
 
You could change the coding slightly so that it doesnt have to be case sensitive. Adding .ToLower to both the values below will correct this.

VB.NET:
            If ExactMatch = True Then
                If tn.Text[COLOR="red"][B].ToLower[/B] [/COLOR]= SearchString[COLOR="Red"][B].ToLower[/B] [/COLOR]Then Return tn
            Else
                If tn.Text.IndexOf(SearchString) <> -1 Then Return tn
            End If
 
Back
Top