none
searching for items in listview with a substring RRS feed

  • Question

  • I'm trying to mimic the Find window that VS implements when the user hits Ctrl-F.  The find is supposed to search either the contents of a ListView or a Tree using a string value.  I am encountering a couple of roadblocks.

    1.  FindItemWithText will only find text that starts with my search string.  I can't believe that they wouldn't implement a sort of .Contains function.  Am i missing something?

    2.  If i do find a listviewitem that matches the search string, how do i set the listviewItem so that it is visible? (i.e. scroll down to put it in view).

    3.  Does anyone have any recommendation on how to keep track of the original starting location (so that i can loop back to the top and continue searching until we reach the original search location?)

     

    thanks anyone/everyone!

     

    Edit:  #2 is solved.

    Wednesday, March 31, 2010 2:44 PM

Answers

  • You'll have to do #1 and #3 manually. They're both really easy though.

    #1. You can use the String.IndexOf function to search strings. Something like:

    For Each item As ListViewItem In ListView1
        If item.Text.IndexOf(searchTerm, StringComparison.CurrentCultureIgnoreCase) > -1 Then
            MessageBox.Show("Here it is: " & item.Text)
        End If
    Next
    #3. Just keep a reference to the starting item/node in a variable, then as you're searching each item after you've looped back through the beginning of the list, just stop when you reach the item that you've saved.
    • Proposed as answer by shawn.ohern Friday, April 2, 2010 4:11 PM
    • Marked as answer by Jing0Moderator Wednesday, April 7, 2010 9:39 AM
    Wednesday, March 31, 2010 3:09 PM

All replies

  • You'll have to do #1 and #3 manually. They're both really easy though.

    #1. You can use the String.IndexOf function to search strings. Something like:

    For Each item As ListViewItem In ListView1
        If item.Text.IndexOf(searchTerm, StringComparison.CurrentCultureIgnoreCase) > -1 Then
            MessageBox.Show("Here it is: " & item.Text)
        End If
    Next
    #3. Just keep a reference to the starting item/node in a variable, then as you're searching each item after you've looped back through the beginning of the list, just stop when you reach the item that you've saved.
    • Proposed as answer by shawn.ohern Friday, April 2, 2010 4:11 PM
    • Marked as answer by Jing0Moderator Wednesday, April 7, 2010 9:39 AM
    Wednesday, March 31, 2010 3:09 PM
  • Thanks shawn,

    I kinda figured i was going to have to do it that way.

    Where i am struggling is with #3.  The reason is that now i have to track 2 different vars.  First is the original starting location, second is the currently found.  In the currently found location, i have to implement logic to look at the next place, or else, i will continuously find the item that i am currently on.  I do this by incrementing the startindex, but only if it is < listview.Items.Count -1.  else, i set it back to 0.

    Seems like a lot to handle for just a simple search.  LIke i mentioned before, i can't believe .Net doesn't have the .Contains as opposed to .Left for string searching.

    Wednesday, March 31, 2010 3:16 PM
  • I'm also choosing to do a for loop instead of hte foreach.  This will allow me to set the starting index of the search.
    Wednesday, March 31, 2010 3:18 PM
  • Yes, this is pretty much how you have to do it if you're going to find one item at a time like you're doing. You need to keep track of where you originally started and where your last match was found.

    Another search model that you may want to look at would be to hide all non-matches and show only the items that match (so the search would be acting like a filter). This will most likely be more work for you, but it may provide a better experience for the user. This seems to be the way that search is moving in a lot of programs.

    Wednesday, March 31, 2010 5:59 PM