Loop Piece of Code While Searching a ListView Control
Here's my code:
VB Code:
Dim SearchString As String
Dim oItem As ListItem
Dim oSubItem As ListSubItem
SearchString = InputBox("", "Search", "Enter Customer Name", 5800, 5240)
For Each oItem In ListView1.ListItems
If InStr(1, oItem.Text, SearchString, vbTextCompare) Then
oItem.Selected = True
oItem.EnsureVisible
ListView1.SetFocus
Exit For
Else
For Each oSubItem In oItem.ListSubItems
If InStr(1, oSubItem.Text, SearchString, vbTextCompare) Then
oItem.Selected = True
oItem.EnsureVisible
ListView1.SetFocus
Exit For
End If
Next
If Not oSubItem Is Nothing Then Exit For
End If
Next
I need to loop a peice of code until it can no longer find the "SearchString".
How would I go about doing that?
Re: Loop Piece of Code While Searching a ListView Control
It is working for me.what is the error you are gettting?
Re: Loop Piece of Code While Searching a ListView Control
I never said there was an error :S
Quote:
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.
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".
Re: Loop Piece of Code While Searching a ListView Control
Anyone else understand this?
Re: Loop Piece of Code While Searching a ListView Control
Quote:
Originally Posted by djwk
Here's my code:
VB Code:
Dim SearchString As String
Dim oItem As ListItem
Dim oSubItem As ListSubItem
SearchString = InputBox("", "Search", "Enter Customer Name", 5800, 5240)
For Each oItem In ListView1.ListItems
If InStr(1, oItem.Text, SearchString, vbTextCompare) Then
oItem.Selected = True
oItem.EnsureVisible
ListView1.SetFocus
Exit For
Else
For Each oSubItem In oItem.ListSubItems
If InStr(1, oSubItem.Text, SearchString, vbTextCompare) Then
oItem.Selected = True
oItem.EnsureVisible
ListView1.SetFocus
Exit For
End If
Next
If Not oSubItem Is Nothing Then Exit For
End If
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.
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
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.
Re: Loop Piece of Code While Searching a ListView Control
VB Code:
'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 :)
Re: Loop Piece of Code While Searching a ListView Control
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.
Re: Loop Piece of Code While Searching a ListView Control
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim lstNew As ListItem
Dim lstSel As ListItem
Dim cnt As Long
For cnt = ListView1.ListItems.Count To 1 Step -1
Set lstSel = ListView1.ListItems(cnt)
If InStr(1, lstSel.Text, "1") > 0 Then
Set lstNew = ListView2.ListItems.Add
lstNew.Text = lstSel.Text
lstNew.Tag = lstSel.Tag
lstNew.SubItems(1) = lstSel.SubItems(1)
ListView1.ListItems.Remove lstSel.Index
End If
Next
Set lstSel = Nothing
Set lstNew = Nothing
End Sub
Private Sub Form_Load()
Dim cnt As Long
Dim lstNew As ListItem
InitLVW ListView1
InitLVW ListView2
For cnt = 1 To 1000 'fill with sample data
Set lstNew = ListView1.ListItems.Add(, , cnt)
lstNew.SubItems(1) = cnt
Next
Set lstNew = Nothing
End Sub
Private Sub InitLVW(ByRef LVWRef As ListView)
With LVWRef
.Checkboxes = False
.ColumnHeaders.Clear
.ColumnHeaders.Add , , "ID"
.ColumnHeaders.Add , , "Test Value"
.Enabled = True
.FullRowSelect = True
.HideSelection = False
.HotTracking = False
.HoverSelection = False
.LabelEdit = lvwManual
.ListItems.Clear
.MultiSelect = True
.Sorted = False
.View = lvwReport
.Visible = True
.Refresh
End With
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.
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:
Dim LstItm As ListItem
Dim Ret As String
Dim NewStart As Long
Ret = InputBox("")
'Search First Column
Set LstItm = ListView1.FindItem(Ret, , 1, 1)
'continue if we find anything
Do While Not LstItm Is Nothing
'Insert here the code to move the items
'Continue Search
NewStart = LstItm.Index + 1
If NewStart > ListView1.ListItems.Count Then Exit Do
Set LstItm = ListView1.FindItem(Ret, , NewStart, 1)
Loop
'Search Subitems
Set LstItm = ListView1.FindItem(Ret, 1, 1, 1)
'continue if we find anything
Do While Not LstItm Is Nothing
'Insert here the code to move the items
'Continue Search
NewStart = LstItm.Index + 1
If NewStart > ListView1.ListItems.Count Then Exit Do
Set LstItm = ListView1.FindItem(Ret, 1, NewStart, 1)
Loop