Question Help create a search on treeview

josephbupe

Registered User.
Local time
Today, 23:15
Joined
Jan 31, 2008
Messages
247
Hi,

I do not know much about treeviews, but having looked at many treeview samples posted on this site and elsewhere on the net, I came across this sample, which I think suits well with what I want to do.

However, I need help to create a search that will ONLY find the product category (not the product name).

I will appreciate any help given.

josephbupe
 

Attachments

I used the TAG property to search through the entire tree. Based on the value in the TAG property, which can be anything you want, i search the tree.

When you create the tree, populate the TAG property with the information you want to search. In your case Type and Product. Type being the catagory and product the productname.

codesnippet:
Code:
Public Function FindNode(tvw As TreeView, strFind As String, intSearchType As eSearchType) As Boolean
'Search for a node using the TAG property.
'TAG property filled like this: node.Tag=First|Second|Third|Fourth|Fifth field
'
' This function first collapses the treeview and then shows the found items in a different color.
    Dim strTag          As String
    Dim lngcount        As Long
    Dim blnFound        As Boolean
    Dim blnActive       As Boolean
    Dim intSearchField  As Integer
    Dim strGlobalSearch As String
    
    blnFound = False
    FindNode = blnFound
    If strFind = "" Then Exit Function
    
    Select Case intSearchType
    Case eSearchType.Pillar
        intSearchField = 3
    Case eSearchType.Fuid
        intSearchField = 4
    Case eSearchType.Section
        intSearchField = 5
    Case Else 'eSearchType.Text
        intSearchField = 0 'Search through complete tag
    End Select

    For lngcount = 1 To tvw.Nodes.Count
        tvw.Nodes(lngcount).Expanded = False
            
        strTag = tvw.Nodes(lngcount).Tag
        strGlobalSearch = Mid$(strTag, InStr(1, strTag, "|"))
        
        blnActive = CBool(GetPart(strTag, "|", 2))
        If InStr(1, strGlobalSearch, strFind) Then Search globally
            If InStr(1, GetPart(strTag, "|", intSearchField), strFind) Then 'Narrow the search
                blnFound = True
                If blnActive Then
                    tvw.Nodes(lngcount).ForeColor = vbBlue 'Found
                Else
                    tvw.Nodes(lngcount).ForeColor = RGB(255, 140, 0) 'DarkOrange'vbGreen 'Found but not active.
                    tvw.Nodes(lngcount).Image = "BottomKey"
                End If
                tvw.Nodes(lngcount).EnsureVisible
            End If
        Else
            If blnActive Then 'Reset previously changed nodecolor.
                tvw.Nodes(lngcount).ForeColor = vbBlack 'Active
            Else
                tvw.Nodes(lngcount).ForeColor = vbRed 'Not active.
                tvw.Nodes(lngcount).Image = "BottomKey"
            End If
        End If
    Next lngcount

    FindNode = blnFound

End Function
GetPart is a function you'll find on this forum. For the images i've used an imagelist.

Share & Enjoy!
 

Users who are viewing this thread

Back
Top Bottom