[RESOLVED] ListView search always returns not found
What am I doing wrong? Everything I attempt on the listview for searching returns no object. Did I set something up wrong? I have tried partial and whole searches and nothing turns up. txtSearch containd "ACT" (without the quotes)
Code:
Private Sub cmdSearch_Click()
Dim oListItem As ListItem
Set oListItem = ListViewFindItem(txtSearch, ListView1, elvSearchText, lvwWhole)
If (oListItem Is Nothing) = False Then
'Found text in listview, now select item
' Set ListView1.SelectedItem = oListItem.Selected
End If
End Sub
Code:
Option Explicit
'The function below searches a listview for a specific item. The code will search the text, subitems and tags of the listview items.
Public Enum elvSearch
elvSearchText = 1
elvSearchSub = 2
elvSearchTag = 4
End Enum
'Purpose : Finds and selects and item in a listview
'Inputs :
' lvFind The listview to search for the item in.
' [eValueType] The type of values to search:
' 1 = Searches the text items.
' 2 = Searches sub items.
' 4 = Searches the item tags.
' [lSearchFor] The type of matching required:
' lvwWhole = Find whole word.
' lvwPartial = Find a partial match.
' [lIndexBeginFrom] The item index to begin the search from, for recursive
' searches. See the index property of the listitem.
' property of the listitem.
'Outputs : N/A
'Author : Andrew Baker
'Date : 25/11/2000 03:17
'Notes :
'Revisions :
Function ListViewFindItem(sFindItem As String, lvFind As ListView, Optional eValueType As elvSearch = elvSearchText + elvSearchSub + elvSearchTag, Optional lSearchFor As Long = lvwPartial, Optional lIndexBeginFrom As Long = 1) As ListItem
On Error Resume Next
'Try to find item
If eValueType And elvSearchText Then
'Search text
Set ListViewFindItem = lvFind.FindItem(sFindItem, lvwText, lIndexBeginFrom, lSearchFor)
End If
If eValueType And elvSearchSub And (ListViewFindItem Is Nothing) Then
'Search subitems
Set ListViewFindItem = lvFind.FindItem(sFindItem, lvwText, lIndexBeginFrom, lSearchFor)
End If
If eValueType And elvSearchTag And (ListViewFindItem Is Nothing) Then
'Search tags
Set ListViewFindItem = lvFind.FindItem(sFindItem, lvwText, lIndexBeginFrom, lSearchFor)
End If
If (ListViewFindItem Is Nothing) = False Then
'Found a matching item, display it.
Set lvFind.SelectedItem = ListViewFindItem
lvFind.SelectedItem.EnsureVisible
End If
On Error GoTo 0
End Function
Yes, I tried every permutation. I did get something to work in a separate project that added the subitems in a little differently. I will attempt that and see what I get.
Private Sub txtSearch_Change()
Dim li As ListItem
Set li = Me.lv.FindItem(Me.txtSearch.Text, 1, , lvwPartial)
If Not li Is Nothing Then
li.Selected = True
li.EnsureVisible
End If
End Sub
I guess that would defeat the FindItem method then...
When I do .Selected = True the background color is a light grey. I did not see anyhting in the properties for that. Is there something I can change the highlight color with?
oListItem.Selected = True does not do this. How is this done?
Did you try
Code:
ListView.EnsureVisible
This what ensure the selected area visible.
Example
Code:
Private Sub txtSearch_Change()
Dim li As ListItem
Set li = Me.lv.FindItem(Me.txtSearch.Text, 1, , lvwPartial)
If Not li Is Nothing Then
li.Selected = True
li.EnsureVisible
End If
End Sub