1 Attachment(s)
[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
Re: ListView search always returns not found
Why not use FindItem directly?
Re: ListView search always returns not found
I tried that first before going to the complicated method thinking that possibly I did not do something correctly. Failed just the same.
It must be the way the listview is loaded...
Re: ListView search always returns not found
When you used FindItem did you use the lvwSubItem parameter since "ACT" seems to be a subitem?
Re: ListView search always returns not found
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.
Re: ListView search always returns not found
I'm confused. subitems are subitems no matter how they are added.
Re: ListView search always returns not found
Well, there seems to be a difference. I will post the two different ways if the recode works...
Re: ListView search always returns not found
Randam,
This simpley works for me.
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
Re: ListView search always returns not found
Re: ListView search always returns not found
I don't know what happened but today it's all working the way it should. Maybe all I needed was a reboot... Nothing else has changed.
But today what I need is to be able to highlight the found selection and bring it into the current viewing window.
oListItem.Selected = True does not do this. How is this done?
Re: ListView search always returns not found
Set the HideSelection property to False.
Re: ListView search always returns not found
It also seems that lvwPartial does not work on a SubItem. It has to be a full match. What's the story on this?
Re: ListView search always returns not found
I don't remember for sure but I think that that's just the way it is. You can always do it manually by looping through the listitems with InStr.
Re: ListView search always returns not found
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?
Re: ListView search always returns not found
Re: ListView search always returns not found
Geez, What did they think you could do with it selected in a grey you can barely see????
Re: ListView search always returns not found
Found it. If you do Listview1.SetFocus, then the selected line turns blue as if it were clicked upon.
1 Attachment(s)
Re: ListView search always returns not found
Is the attached of any use to you? (You'll need to supply the NorthWind database.
Re: ListView search always returns not found
Quote:
Originally Posted by randem
and bring it into the current viewing window.
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
Re: ListView search always returns not found
Re: [RESOLVED] ListView search always returns not found
BTW: I had to loop thru the SubItems and do an Instr to find partials...