Results 1 to 12 of 12

Thread: Disabling automatic listview find

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Geneva, Switzerland
    Posts
    9

    Disabling automatic listview find

    I've just finished writing a routine to search for a row in my listview based on the character that the user types over the given column. I identify the column the mouse is over (MouseMove) and trap the character typed in (KeyPress) and then in a loop I compare the letter typed with the fist character of the subitem (corresponding to the column the mouse is over) in all listitems. If a match is found, the listitem is selected and I ensure it's visible. My problem is that the built-in feature that searches the Listview1.ListItems(x).Text for the typed character, does its own search after mine. The result is that it then positions itself to the first occurence of the character in Listview1.ListItems(x).Text, immediately after the occurence in the other column that I was searching on.
    Listview columns and data:

    Component ID_ CAS Number
    29___________ 100-06-1
    244___________ 112-30-1
    250___________ 112-53-8
    446___________ 120-14-9
    188___________ 121-33-5

    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.

    It seems that the only way to prevent this is to disable that automatic feature. Anyone know how? Any other ideas?

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Geneva, Switzerland
    Posts
    9
    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.
    VB Code:
    1. Private Sub lvwComponents_KeyDown(KeyCode As Integer, Shift As Integer)
    2.  
    3.    Dim itmX As ListItem, x As Integer, y As Integer, strSearchString As String
    4.  
    5.    ' Exit if nothing to search.
    6.    If strLVW_DBColumnName = vbNullString _
    7.    Or lvwComponents.ListItems.Count = 0 Then Exit Sub
    8.  
    9.    ' If mouse over a column, search for the letter pressed.
    10.    With lvwComponents
    11.       If strLVW_DBColumnName = "COMPONENT_ID" Then
    12.          Set itmX = .FindItem(Chr$(KeyAscii), lvwText, , lvwPartial)
    13.       Else
    14.          Set itmX = .FindItem(Chr$(KeyAscii), lvwSubItem, _
    15.             .ColumnHeaders(strLVW_DBColumnName).Index)
    16.       End If
    17.       If Not itmX Is Nothing Then
    18.          itmX.Selected = True
    19.          itmX.EnsureVisible
    20.       End If
    21.    End With
    22. End Sub
    Since FindItem didn't work, I coded a loop....
    VB Code:
    1. Private Sub lvwComponents_KeyDown(KeyCode As Integer, Shift As Integer)
    2.  
    3.    Dim itmX As ListItem, x As Integer, y As Integer, strSearchString As String
    4.  
    5.    ' Exit if nothing to search.
    6.    If strLVW_DBColumnName = vbNullString _
    7.    Or lvwComponents.ListItems.Count = 0 Then Exit Sub
    8.  
    9.    ' If mouse over a column, search for the letter pressed.
    10.    With lvwComponents
    11.       For x = 1 To .ListItems.Count
    12.          ' Use text or subitem as search string depending on column that mouse is over.
    13.          If strLVW_DBColumnName = "COMPONENT_ID" Then
    14.             strSearchString = Left$(.ListItems(x).Text, 1)
    15.          Else
    16.             y = .ColumnHeaders(strLVW_DBColumnName).Index - 1
    17.             strSearchString = Left$(.ListItems(x).SubItems(y), 1)
    18.          End If
    19.          ' Search text/subitem.
    20.          If UCase(strSearchString) = UCase(Chr$(KeyAscii)) Then
    21.             .ListItems(x).Selected = True
    22.             .ListItems(x).EnsureVisible
    23.             Exit Sub
    24.          End If
    25.       Next x
    26.    End With  
    27. 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.

    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
    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.

  4. #4

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by MartinLiss
    One problem you have is that there is no "KeyAscii" parameter in the KeyDown event. I'm looking at the rest of the coide now.
    there is a keycode though

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Geneva, Switzerland
    Posts
    9
    Sorry I didn't clean up the code a bit more. I switched from KeyPress to KeyDown, so I included the following lines.

    Dim KeyAscii As Integer
    KeyAscii = KeyCode

  7. #7

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Geneva, Switzerland
    Posts
    9
    If you mean the Item and SubItem key, I don't fill them in, so I guess they don't have any value.

  9. #9

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Geneva, Switzerland
    Posts
    9
    If I use the loop, then I need to suppress the automatic search on the Item column, COMPONENT_ID. Do you know if this is possible?

  11. #11

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Geneva, Switzerland
    Posts
    9

    Smile Disabling automatic listview find [Resolved]

    It works! You've just made some ladies in Germany (my users) very happy.

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