Results 1 to 31 of 31

Thread: ListView.FindItemWithText() Help

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    ListView.FindItemWithText() Help

    Alright, I did a search on vb forums and found something that helped me out:

    vb Code:
    1. Dim itmX As ListViewItem = lstStudents.FindItemWithText(tstSearch.Text, False, 0)
    2.         If Not itmX Is Nothing Then
    3.             lstStudents.Focus()
    4.             itmX.Selected = True
    5.             lstStudents.Items(itmX.Index).Selected = True
    6.             itmX.EnsureVisible()
    7.         End If

    So here's the problem know. When have more than 1 of the same text (say, Last Name or First Name) Then it will only do the very last item, instead of starting with the FIRST item & then when I click the search Button again, it will go to the next item in the List with that Text.

  2. #2
    Member
    Join Date
    Oct 2008
    Posts
    40

    Re: ListView.FindItemWithText() Help

    This what you are on abouts? This all goes into the "Search/Find" button what ever you use.

    Searching Code:
    1. Dim index As Integer
    2.         Dim maxindex As Integer
    3.         Dim searchcriteria As String
    4.         Dim found As Boolean = False
    5.  
    6.         searchcriteria = TextBox3.Text
    7.         maxindex = lstDisplay.Items.Count
    8.         lstDisplay.Focus()
    9.  
    10.         Do While (found = False)
    11.  
    12.             If index > maxindex Then
    13.                 MsgBox("Can't Find")
    14.             Else
    15.                 If searchcriteria = lstDisplay.SelectedItem Then
    16.                     found = True
    17.                 Else
    18.                     index = index + 1
    19.                     lstDisplay.SelectedIndex = index
    20.                 End If
    21.             End If
    22.         Loop

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    No. I'm looking for LISTVIEw.FindItemWithText not a ListBox.

  4. #4
    Member
    Join Date
    Oct 2008
    Posts
    40

    Re: ListView.FindItemWithText() Help

    I've never came accross this before but foudn this.


    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

  5. #5
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    I don't know why it would select the last item but if you want to make it select the next item with the same search text you will need to keep track of the last search text and the last item/index that you found with that search. That way if the user presses the button again with the same search you can pick up where you left off...

    something like this:

    Code:
    If lastSearch = tstSearch.Text Then
       itmX  = lstStudents.FindItemWithText(tstSearch.Text, False, lastIndex + 1)
    Else
       itmX = lstStudents.FindItemWithText(tstSearch.Text, False, 0)
    End If
    
    lastIndex = itmX.Index
    lastSearch = tstSearch.Text
    
    'your other code...

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Still need some help, can't figure it out, neither of those code are working properly.

  7. #7
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    Can you clarify what you're looking for? As I understand it, you have a ListView which you would like to search. It's possible that the listview contains multiple items with the same text value. When this is the case when the user searches you would like to highlight the first item. Then if the user performs the same search again by clicking the button again you want to highlight the second item in the listview that matches the search. Is this right?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Exactly wy125, Thats what I'm trying to accomplish

  9. #9
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    Re: ListView.FindItemWithText() Help

    I'd have to go with what wy125 says, his code looks okay, and I don't know why that shouldn't work.

    I'm not quite sure what you want, do you want it to find the last item first, or is that what the program does, and you don't want that at all? From your question, I think you want it to find the last result.
    If so, I think you should use a loop to go through all the results, and remember the last index, and then use listview.findItemWithText(seach.text,false,lastindex)

    If not: I haven't got a clue why it gives you the last result instead of the first.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Thank You

    Heres the code I used:

    vb Code:
    1. If lastsearch = tstSearch.Text Then
    2.             If lastindex <= lstStudents.Items.Count - 2 Then
    3.                 itmX = lstStudents.FindItemWithText(tstSearch.Text, False, lastindex + 1)
    4.             Else
    5.                 itmX = lstStudents.FindItemWithText(tstSearch.Text, False, 0)
    6.             End If
    7.         Else
    8.             itmX = lstStudents.FindItemWithText(tstSearch.Text, False, 0)
    9.         End If
    10.         lstStudents.Items(itmX.Index).Selected = True
    11.         lastindex = itmX.Index
    12.         lastsearch = tstSearch.Text

  11. #11
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    okay i just whipped together something so i could test it.. It seems to work pretty well. You will probably want to tweak this to get it to do exactly what you want.

    First you need to have two fields in your class, one for the lastSearch text and the other for the index of the last item found:


    Code:
    Public Class Form1
    
        Private lastSearch As String
        Private lastIndex As Integer = -1
    
       ....

    then when the user clicks the button you can do something like this:

    vb Code:
    1. Dim searchText As String = txtSearch.Text.Trim()
    2.         Dim lvItem As ListViewItem = Nothing
    3.  
    4.  
    5.         'don't bother with empty searches
    6.         If searchText = "" Then
    7.             Return
    8.         End If
    9.  
    10.         'clear previously selected itmes
    11.         If (listView.SelectedItems.Count > 0) Then
    12.             listView.SelectedItems(0).Selected = False
    13.         End If
    14.  
    15.         'check if the lastSearch is the same as current one
    16.         'also check if we've reached the last item
    17.         'the second check, lastIndex < listView.Items.Count - 1, makes sure we don't
    18.         'go beyond the indices of the listview
    19.         'the way it is now, if we reach the last item in the search it will go back to the
    20.         'first item
    21.         If searchText = lastSearch And lastIndex < listView.Items.Count - 1 Then
    22.  
    23.             'we need to continue where we left off, search 1 ahead of where we last found
    24.             'the item of the same name
    25.             lvItem = listView.FindItemWithText(searchText, False, lastIndex + 1)
    26.  
    27.         Else
    28.             'start the search over from the beginning
    29.             lvItem = listView.FindItemWithText(searchText, False, 0)
    30.  
    31.         End If
    32.  
    33.  
    34.             lastSearch = searchText
    35.  
    36.             If Not (lvItem Is Nothing) Then
    37.                 lastIndex = lvItem.Index
    38.                 listView.Focus()
    39.                 lvItem.Selected = True
    40.  
    41.  
    42.  
    43.             End If

  12. #12
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    Quote Originally Posted by Wesley008
    Thank You

    Heres the code I used:

    vb Code:
    1. If lastsearch = tstSearch.Text Then
    2.             If lastindex <= lstStudents.Items.Count - 2 Then
    3.                 itmX = lstStudents.FindItemWithText(tstSearch.Text, False, lastindex + 1)
    4.             Else
    5.                 itmX = lstStudents.FindItemWithText(tstSearch.Text, False, 0)
    6.             End If
    7.         Else
    8.             itmX = lstStudents.FindItemWithText(tstSearch.Text, False, 0)
    9.         End If
    10.         lstStudents.Items(itmX.Index).Selected = True
    11.         lastindex = itmX.Index
    12.         lastsearch = tstSearch.Text

    that actually looks okay. Make sure you are giving the listview focus or alternatively set listview.HideSelection = False

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    vb Code:
    1. If Not (lvItem Is Nothing) Then
    2.                 lastIndex = lvItem.Index
    3.                 listView.Focus()
    4.                 lvItem.Selected = True
    5.             End If


    What does this IF Statment Do?

  14. #14
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    it checks to make sure that lvItem isn't null (the search turned up empty). If it was null and you tried to read the index and highlight it you would get an exception.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Hey, I ran into a glitch in this code:


    vb Code:
    1. Public Sub Students_Find()
    2.         With frmStudents
    3.             If .tstSearch.Text = vbNullString Then
    4.                 Return
    5.             End If
    6.             If .lstStudents.SelectedItems.Count > 0 Then
    7.                 .lstStudents.SelectedItems(0).Selected = False
    8.             End If
    9.             If LastSearch = .tstSearch.Text And LastIndex < .lstStudents.Items.Count - 1 Then
    10.                 Items = .lstStudents.FindItemWithText(.tstSearch.Text, True, LastIndex + 1)
    11.             Else
    12.                 Items = .lstStudents.FindItemWithText(.tstSearch.Text, True, 0)
    13.             End If
    14.             LastSearch = .tstSearch.Text
    15.             If Not (Items Is Nothing) Then
    16.                 LastIndex = Items.Index
    17.                 .lstStudents.Focus()
    18.                 .lstStudents.Items(Items.Index).Selected = True
    19.             End If
    20.         End With
    21.     End Sub

    What happens is when I have 3+ Items (it work fine) but when I have 2 Items it Selects the First then the Second but doesn't go back to the Beginning like the 3+ Items.

    How can I fix this? I know I need to set LastIndex = -1 somewhere but I can't figure it out.

  16. #16
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    You are right, you can set lastIndex to -1 after you don't find an item:

    Code:
    If Not (Items is Nothing) Then
    ...
    
    Else
         LastIndex = -1
    End If

    By the way, this will cause a "skip" in the rotation before it goes back to the beginning. At one point nothing will be highlighted and then it will return to the beginning. You will have to press the button twice to get to go back to the beginning. If that is okay then you can leave it as it is, otherwise, I would recommend changing the way it works. Instead of doing it this way, I would keep a List of ListView items that match the current search and just change the index of the currently shown one in the list. That will allow you to rotate through them without getting a skip. When a new search is done make a new list that matches the search and rotate about that list.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    I see what you mean by a skip, but I don't understand how I would fix it.

    Oh, I think I might know what you mean:

    Say I have

    A
    A
    A
    B
    B
    A

    And I want to Search for "A"

    It does a Loop to Hide the "B"'s and then just shows the A's ?

    Is that what you mean? I tried doing this with some For Statements but I got confused on how I would check to see if the Item Contained that String.

  18. #18
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    Not exactly... I was thinking that keeping a list of the ListViewItems that match the current search. When it's the same search you can just cycle through that list and highlight accordingly. Something like this:

    Code:
    Public Class Form
    Private lastSearch As String   'the last text the user searched for
    Private currentIndex = 0        'the current index to highlight
    Private matchingList As List(Of ListViewItem) = New List(Of ListViewItem)  ' the list of matching ListViewItems
    
    ....

    Then make a couple methods; one to fill the list on a new search and the other to highlight the next item in the list

    vb Code:
    1. Private Sub FillMatchingList(ByVal txt As String)
    2.        
    3.         'clear the list, because we are starting fresh
    4.         matchingList.Clear()
    5.  
    6.         Dim listViewItem As ListViewItem
    7.         Dim lastIndex As Integer = -1
    8.  
    9.  
    10.         listViewItem = Nothing
    11.  
    12.  
    13.         ' keep going until we have found all matches
    14.         Do Until lastIndex >= ListView1.Items.Count - 1
    15.             listViewItem = ListView1.FindItemWithText(txt, False, lastIndex + 1)
    16.  
    17.             If Not (listViewItem Is Nothing) Then
    18.                 matchingList.Add(listViewItem)
    19.                 lastIndex = listViewItem.Index
    20.             Else 'we are done finding matches
    21.                 Return
    22.             End If
    23.  
    24.         Loop
    25.  
    26.     End Sub
    27.  
    28.  
    29.     'highlights the next item in the list
    30.     Private Sub HighlightNextMatch()
    31.  
    32.  
    33.         'clear the current selection
    34.         If (ListView1.SelectedItems.Count > 0) Then
    35.             ListView1.SelectedItems(0).Selected = False
    36.  
    37.         End If
    38.  
    39.  
    40.  
    41.         matchingList(currentIndex).Selected = True
    42.         currentIndex = (currentIndex + 1) Mod matchingList.Count
    43.         ListView1.Focus()
    44.  
    45.     End Sub


    then in your button click handler:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         Dim searchText As String = TxtSearch.Text.Trim()
    4.  
    5.         If searchText <> lastSearch Then
    6.             lastSearch = searchText
    7.             currentIndex = 0
    8.             FillMatchingList(searchText)
    9.         End If
    10.  
    11.  
    12.         HighlightNextMatch()
    13.  
    14.  
    15.      
    16.  
    17.     End Sub
    Last edited by wy125; Nov 2nd, 2008 at 09:43 AM.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Thank you so Much I tweaked your code to turn it into a Function so I can keep calling it over again for different objects. Thanks again Works like a Charm.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Hey Wy125. I got another issue, a bit simpler.

    I have 1 List View on 1 Form and another List View on another Form.

    So now, if I search for say "A" on the First Form, then try to search "A" on the Second Form it doesn't work.


    Here's the code I'm using:

    Code:
        Public Function FindInList(ByVal ListName As ListView, ByVal SearchText As String) As Boolean
            With frmStudents
                matchingList.Clear()
                Dim listViewItem As ListViewItem
                Dim lastIndex As Integer = -1
                listViewItem = Nothing
                Do Until lastIndex >= ListName.Items.Count - 1
                    listViewItem = ListName.FindItemWithText(SearchText, True, lastIndex + 1)
                    If Not (listViewItem Is Nothing) Then
                        matchingList.Add(listViewItem)
                        lastIndex = listViewItem.Index
                    Else
                        Return Nothing
                    End If
                Loop
            End With
        End Function
    
        Public Function HighLightInList(ByVal ListName As ListView) As Boolean
            If (ListName.SelectedItems.Count > 0) Then
                ListName.SelectedItems(0).Selected = False
            End If
            matchingList(currentIndex).Selected = True
            currentIndex = (currentIndex + 1) Mod matchingList.Count
            ListName.Focus()
        End Function
    
        Public Sub Overview_Assign_Find() 'FORM 1 SEARCH
            With frmOverview
                Dim searchText As String = .txtAssigmentSearch.Text '.Trim()
                If searchText <> lastSearch Then
                    lastSearch = searchText
                    currentIndex = 0
                    FindInList(.lstAssignments, .txtAssigmentSearch.Text)
                End If
                HighLightInList(.lstAssignments)
            End With
        End Sub
    
        Public Sub Assign_Find() 'FORM 2 SEARCH
            With frmAssignments
                Dim searchText As String = .tstSearch.Text '.Trim()
                If searchText <> lastSearch Then
                    lastSearch = searchText
                    currentIndex = 0
                    FindInList(.lstAssign, .tstSearch.Text)
                End If
                HighLightInList(.lstAssign)
            End With
        End Sub

  21. #21
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    it compiles? You duplicated everything on the second form that you have on the first? You are not trying to call one form's code from the other are you?

    There's no reason that it should not work if you've duplicated everything.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    The Code Works, Only if I do this:


    First Listview:

    I search for A

    Second Listview

    I search for B

    But I search for A on the First and A on the Second, the Second Search for A will not Select. Theirs no Error - its just selecting the ITEM. I did some TEST to check if it trying to change the Correct Listview and it is.

  23. #23
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    okay. just so I understand you have two separate forms and you have a listview on each and you want to be able to do independent searches right?

    could you post your code showing the actual classes, the member declarations (just the searchtext and list), the methods in each form?

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    You wouldn't happen to have MSN ? I could send you the Project File which would help probably a lot more.

    Add:


  25. #25
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    you can attach it here if you like, i'm on my linux machine and don't have an msn client setup yet... Look at the "attach files" section when you are posting... zip up your project and attach the code

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Alright here it is:


    Heres what your going want to look at:

    frmOverview [The 2 Listviews (Students and Assignments)]
    modFindings [All]
    frmStudents [The Search Button on the Bottom Right]
    frmAssignments [The Search Button on the Bottom Right]
    Attached Files Attached Files

  27. #27
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: ListView.FindItemWithText() Help

    Here is an example. The Find method will get the sub item of the listView control with the specified text:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As ListViewItem.ListViewSubItem = Me.Find(Me.ListView1, "d")
        End Sub
    
        Private Function Find(ByVal lv As ListView, ByVal text As String) As ListViewItem.ListViewSubItem
            For Each lvi As ListViewItem In lv.Items
                For Each lvsi As ListViewItem.ListViewSubItem In lvi.SubItems
                    If lvsi.Text = text Then
                        Return lvsi
                    End If
                Next lvsi
            Next lvi
            Return Nothing
        End Function
    
    End Class

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Wy125 Anything yet with the Project?

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Hey wy125, I fixed this issue, I just called a new SUB ClearFindings every time I go to a New Form & I clear the Following:

    vb Code:
    1. Public Sub ClearFindings()
    2.         lastSearch = vbNullString
    3.         currentIndex = 0
    4.         matchingList.Clear()
    5.     End Sub

    ANd it works:

    But now heres something that has been bothering me for the longest time. How do I figure out if the text I put into the Text Box is somewhere in the ListView? It was easy with a ListBox I just did a .Contains but I can't figure out for a ListView. This causes my other problem with when no text can be found it gives me a Error with the IndexoutofRange.

    So short and simple:

    How do I check if the Input Text Is In the ListView?

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: ListView.FindItemWithText() Help

    Hey, I figured it out. I need to add a NAME to each Item I add and then use .ContainsKey(NAME) to check if its in their.

  31. #31
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: ListView.FindItemWithText() Help

    Hey sorry man, was out of the house for a while. I'm glad you figured it out. BTW your interface is very nice. My GUIs are always so ugly...

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