|
-
Mar 3rd, 2003, 01:23 AM
#1
Thread Starter
New Member
Searching listviews
Could someone plz tell me how to search a listview for a certain string that has been entered into it. I want to have an inputbox first that asks for the word to search for and then the code searches the listview for that string. If it has been found then a msgbox will come up to saying that it is found.
-
Mar 3rd, 2003, 06:39 AM
#2
Fanatic Member
This is what I use to search a listview.
This only searches the listitem text, It does not search subitems, To search subitems you will have to loop through the listview a cell at a time using 2 loops.
Code:
Private Sub FindAnItem(strItem As String, aosp As String)
Dim itmFound As ListItem ' FoundItem variable.
Set itmFound = lvwAOS.FindItem(strItem)
If itmFound Is Nothing Then ' If no match, inform user and exit.
MsgBox "Item Not found"
Else
' highlight the item '
itmFound.EnsureVisible ' Scroll ListView to show found ListItem.
itmFound.Selected = True ' Select the ListItem.
End If
End Sub
I had to change the above code because the listview had more than one listitem with the same text. If a match was found I had to check the first subitem.
Code:
Private Sub FindAnItem(strItem As String, aosp As String)
Dim itmFound As ListItem ' FoundItem variable.
Dim intIndex As Integer
Set itmFound = lvwAOS.FindItem(strItem)
If itmFound Is Nothing Then ' If no match, inform user and exit.
' Item Not found
Else
' highlight the item '
Do While (Trim(lvwAOS.SelectedItem.SubItems(1)) <> Trim(aosp) Or Trim(lvwAOS.SelectedItem.Text) <> Trim(strItem))
intIndex = lvwAOS.SelectedItem.Index
Set lvwAOS.SelectedItem = lvwAOS.ListItems(intIndex + 1)
Loop
End If
End Sub
hope this helps
-
Mar 3rd, 2003, 11:05 PM
#3
Thread Starter
New Member
Thanks for the code but there still is a problem with the first lot of code you gave me. When it searches the listview for a string that i enter it does find it but doesn't display the found item. Also when i enter a string that doesn't exist then it does come up saying item not found. Plz Help
-
Mar 4th, 2003, 04:35 AM
#4
Fanatic Member
If a string doesn't exist it should come up saying 'item not found', shouldn't it? If you want it to display a different message change the code, it is a messagebox command.
As for not displaying found items check the HideSelection property. It is usually set true by default. It needs to be set to false.
-
Mar 4th, 2003, 06:57 PM
#5
Thread Starter
New Member
The top code works but i need to be able to search all subitems and then display the result. I am using this code to search:
Private Sub FindRecord()
Dim SearchText As String
Dim itmFound As ListItem
SearchText = InputBox("Enter the string to search for", "Search")
Set itmFound = lvInfo.FindItem(SearchText)
If itmFound Is Nothing Then
MsgBox "Item not found", vbCritical, "Error"
Else
itmFound.EnsureVisible
itmFound.Selected = True
End If
End Sub
Please help me!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|