Results 1 to 13 of 13

Thread: Loop Piece of Code While Searching a ListView Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Loop Piece of Code While Searching a ListView Control

    Here's my code:
    VB Code:
    1. Dim SearchString As String
    2. Dim oItem As ListItem
    3. Dim oSubItem As ListSubItem
    4.                          
    5. SearchString = InputBox("", "Search", "Enter Customer Name", 5800, 5240)
    6.  
    7. For Each oItem In ListView1.ListItems
    8.     If InStr(1, oItem.Text, SearchString, vbTextCompare) Then
    9.         oItem.Selected = True
    10.         oItem.EnsureVisible
    11.         ListView1.SetFocus
    12.         Exit For
    13.     Else
    14.         For Each oSubItem In oItem.ListSubItems
    15.             If InStr(1, oSubItem.Text, SearchString, vbTextCompare) Then
    16.                 oItem.Selected = True
    17.                 oItem.EnsureVisible
    18.                 ListView1.SetFocus
    19.                 Exit For
    20.             End If
    21.         Next
    22.         If Not oSubItem Is Nothing Then Exit For
    23.     End If
    24. Next


    I need to loop a peice of code until it can no longer find the "SearchString".

    How would I go about doing that?

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Loop Piece of Code While Searching a ListView Control

    It is working for me.what is the error you are gettting?
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Re: Loop Piece of Code While Searching a ListView Control

    I never said there was an error :S

    I need to loop a peice of code until it can no longer find the "SearchString".

    How would I go about doing that?
    EDIT: Basically I have some code that removes the selected item from listview1 and put into listview2 (after a search) and I need this code to continue looping until it can't find the original search string.
    Last edited by djwk; Jan 3rd, 2007 at 05:41 AM.

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Loop Piece of Code While Searching a ListView Control

    I couldn't able to understand this
    I need to loop a peice of code until it can no longer find the "SearchString".
    Please mark you thread resolved using the Thread Tools as shown

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Re: Loop Piece of Code While Searching a ListView Control

    Anyone else understand this?

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Loop Piece of Code While Searching a ListView Control

    Quote Originally Posted by djwk
    Here's my code:
    VB Code:
    1. Dim SearchString As String
    2. Dim oItem As ListItem
    3. Dim oSubItem As ListSubItem
    4.                          
    5. SearchString = InputBox("", "Search", "Enter Customer Name", 5800, 5240)
    6.  
    7. For Each oItem In ListView1.ListItems
    8.     If InStr(1, oItem.Text, SearchString, vbTextCompare) Then
    9.         oItem.Selected = True
    10.         oItem.EnsureVisible
    11.         ListView1.SetFocus
    12.         Exit For
    13.     Else
    14.         For Each oSubItem In oItem.ListSubItems
    15.             If InStr(1, oSubItem.Text, SearchString, vbTextCompare) Then
    16.                 oItem.Selected = True
    17.                 oItem.EnsureVisible
    18.                 ListView1.SetFocus
    19.                 Exit For
    20.             End If
    21.         Next
    22.         If Not oSubItem Is Nothing Then Exit For
    23.     End If
    24. Next


    I need to loop a peice of code until it can no longer find the "SearchString".

    How would I go about doing that?
    If your listview is set to MultiSelect then code above already selects the appropriate listitems... you can do another loop to collect ListItems with Selected = True to transfer them to the other listview, OR you can integrate the transfer (transfer immediately when found) in the loop above. Whichever is easier for you to do.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Re: Loop Piece of Code While Searching a ListView Control

    Quote Originally Posted by leinad31
    If your listview is set to MultiSelect then code above already selects the appropriate listitems... you can do another loop to collect ListItems with Selected = True to transfer them to the other listview, OR you can integrate the transfer (transfer immediately when found) in the loop above. Whichever is easier for you to do.
    After setting multiselect to true it just slects the originally selected item before the search and the first item it finds during the search.

    None of the others are selected :S

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Loop Piece of Code While Searching a ListView Control

    Well before you do the search you have to unset them of course... otherwise the search result will be cluttered with user selected items.

    Code:
    For Each oItem In ListView1.ListItems
       oItem.Selected = False
    Or skip setting their Selected property and do the transfer immediately if search criteria is matched.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Re: Loop Piece of Code While Searching a ListView Control

    VB Code:
    1. 'If Not oSubItem Is Nothing Then Exit For

    I just commented out that line of code and it works fine. Now all I need to do is move the selected items over to a new listview

    EDIT: And also thanks leinad, I just noticed that problem and your code worked
    Last edited by djwk; Jan 4th, 2007 at 05:03 AM.

  10. #10
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Loop Piece of Code While Searching a ListView Control

    Have a Look at this
    Please mark you thread resolved using the Thread Tools as shown

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Re: Loop Piece of Code While Searching a ListView Control

    Quote Originally Posted by leinad31
    Or skip setting their Selected property and do the transfer immediately if search criteria is matched.
    Just wondering how I would do that, since the way I'm thinking of doing it would be a really long way round.

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Loop Piece of Code While Searching a ListView Control

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim lstNew As ListItem
    5. Dim lstSel As ListItem
    6. Dim cnt As Long
    7.  
    8.    For cnt = ListView1.ListItems.Count To 1 Step -1
    9.       Set lstSel = ListView1.ListItems(cnt)
    10.       If InStr(1, lstSel.Text, "1") > 0 Then
    11.          Set lstNew = ListView2.ListItems.Add
    12.          lstNew.Text = lstSel.Text
    13.          lstNew.Tag = lstSel.Tag
    14.          lstNew.SubItems(1) = lstSel.SubItems(1)
    15.          ListView1.ListItems.Remove lstSel.Index
    16.       End If
    17.    Next
    18.    
    19.    Set lstSel = Nothing
    20.    Set lstNew = Nothing
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24. Dim cnt As Long
    25. Dim lstNew As ListItem
    26.  
    27.    InitLVW ListView1
    28.    InitLVW ListView2
    29.    For cnt = 1 To 1000  'fill with sample data
    30.       Set lstNew = ListView1.ListItems.Add(, , cnt)
    31.       lstNew.SubItems(1) = cnt
    32.    Next
    33.    
    34.    Set lstNew = Nothing
    35. End Sub
    36.  
    37. Private Sub InitLVW(ByRef LVWRef As ListView)
    38.    With LVWRef
    39.       .Checkboxes = False
    40.       .ColumnHeaders.Clear
    41.       .ColumnHeaders.Add , , "ID"
    42.       .ColumnHeaders.Add , , "Test Value"
    43.       .Enabled = True
    44.       .FullRowSelect = True
    45.       .HideSelection = False
    46.       .HotTracking = False
    47.       .HoverSelection = False
    48.       .LabelEdit = lvwManual
    49.       .ListItems.Clear
    50.       .MultiSelect = True
    51.       .Sorted = False
    52.       .View = lvwReport
    53.       .Visible = True
    54.      
    55.       .Refresh
    56.    End With
    57. End Sub
    You will have to iterate from end to start to avoid problems with indices shifting after deletion. Listview2 will have items in reverse order compared to items order in listview1. If that's ok then just update sample above.

  13. #13
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Loop Piece of Code While Searching a ListView Control

    Instead of looping through each item in the listview, you can use its built in FindItem command to search for the required strings.

    VB Code:
    1. Dim LstItm As ListItem
    2. Dim Ret As String
    3. Dim NewStart As Long
    4.  
    5. Ret = InputBox("")
    6.  
    7. 'Search First Column
    8. Set LstItm = ListView1.FindItem(Ret, , 1, 1)
    9. 'continue if we find anything
    10. Do While Not LstItm Is Nothing
    11.     'Insert here the code to move the items
    12.  
    13.     'Continue Search
    14.     NewStart = LstItm.Index + 1
    15.     If NewStart > ListView1.ListItems.Count Then Exit Do
    16.     Set LstItm = ListView1.FindItem(Ret, , NewStart, 1)
    17. Loop
    18.  
    19. 'Search Subitems
    20. Set LstItm = ListView1.FindItem(Ret, 1, 1, 1)
    21. 'continue if we find anything
    22. Do While Not LstItm Is Nothing
    23.     'Insert here the code to move the items
    24.  
    25.     'Continue Search
    26.     NewStart = LstItm.Index + 1
    27.     If NewStart > ListView1.ListItems.Count Then Exit Do
    28.     Set LstItm = ListView1.FindItem(Ret, 1, NewStart, 1)
    29. Loop

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