Yes, I looked at the other thread but it didn't help. I've tried the FindItem method, but I couldn't get it to work with Subitems, only with the Item.Text. My objective is have the user type in a letter over any column in the listview (approx. 10 columns), and have it search in that specific item/subitem for a value that starts with that letter, select the item and position to the item.
Here's my code for FindItem. strLVW_DBColumnName is the listview column header name. I've tried this with KeyDown and KeyPress, both without success.
Since FindItem didn't work, I coded a loop....VB Code:
Private Sub lvwComponents_KeyDown(KeyCode As Integer, Shift As Integer) Dim itmX As ListItem, x As Integer, y As Integer, strSearchString As String ' Exit if nothing to search. If strLVW_DBColumnName = vbNullString _ Or lvwComponents.ListItems.Count = 0 Then Exit Sub ' If mouse over a column, search for the letter pressed. With lvwComponents If strLVW_DBColumnName = "COMPONENT_ID" Then Set itmX = .FindItem(Chr$(KeyAscii), lvwText, , lvwPartial) Else Set itmX = .FindItem(Chr$(KeyAscii), lvwSubItem, _ .ColumnHeaders(strLVW_DBColumnName).Index) End If If Not itmX Is Nothing Then itmX.Selected = True itmX.EnsureVisible End If End With End Sub
VB Code:
Private Sub lvwComponents_KeyDown(KeyCode As Integer, Shift As Integer) Dim itmX As ListItem, x As Integer, y As Integer, strSearchString As String ' Exit if nothing to search. If strLVW_DBColumnName = vbNullString _ Or lvwComponents.ListItems.Count = 0 Then Exit Sub ' If mouse over a column, search for the letter pressed. With lvwComponents For x = 1 To .ListItems.Count ' Use text or subitem as search string depending on column that mouse is over. If strLVW_DBColumnName = "COMPONENT_ID" Then strSearchString = Left$(.ListItems(x).Text, 1) Else y = .ColumnHeaders(strLVW_DBColumnName).Index - 1 strSearchString = Left$(.ListItems(x).SubItems(y), 1) End If ' Search text/subitem. If UCase(strSearchString) = UCase(Chr$(KeyAscii)) Then .ListItems(x).Selected = True .ListItems(x).EnsureVisible Exit Sub End If Next x End With End Sub
The loop above seems to work fine on alphanumeric data. However, one of my subitems (CAS_NUMBER column) is numeric and with that column it seems to find the correct match in the subitem column and then jumps to the first match in the ListItems(x).Text directly following the match that it should have found. Note: COMPONENT_ID column (ListItems(x).Text) is numeric. This is why I'm assuming that the tricky automatic search feature is overriding my search loop when I'm searching on numeric data. My example again.
If the user types 1 over the CAS Number column it positions to Component ID 188. I've tested this with various numbers typed in and it consistently behaves this way. Typing letters in for others columns doesn't have this problem, presumably since the auto search cannot find an alphabetic value in the Component ID column.Code:Component ID CAS Number 29 100-06-1 244 112-30-1 250 112-53-8 446 120-14-9 188 121-33-5 232 121-33-5




Reply With Quote