Results 1 to 21 of 21

Thread: [RESOLVED] ListView search always returns not found

  1. #1

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Resolved [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
    Attached Images Attached Images  

  2. #2

  3. #3

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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...

  4. #4

  5. #5

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

  6. #6

  7. #7

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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...

  8. #8
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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

  9. #9

  10. #10

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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?

  11. #11

  12. #12

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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?

  13. #13

  14. #14

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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?

  15. #15

  16. #16

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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????

  17. #17

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

  18. #18

  19. #19
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    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

  20. #20

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: ListView search always returns not found

    Yes, I have it

    Thanks.

  21. #21

    Thread Starter
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: [RESOLVED] ListView search always returns not found

    BTW: I had to loop thru the SubItems and do an Instr to find partials...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width