|
-
Aug 13th, 2004, 11:55 AM
#1
Thread Starter
New Member
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?
-
Aug 13th, 2004, 03:04 PM
#2
-
Aug 16th, 2004, 04:03 AM
#3
Thread Starter
New Member
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:
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
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
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.
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.
-
Aug 16th, 2004, 09:33 AM
#4
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.
-
Aug 16th, 2004, 09:37 AM
#5
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
-
Aug 16th, 2004, 09:38 AM
#6
Thread Starter
New Member
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
-
Aug 16th, 2004, 10:03 AM
#7
Is your column Key = COMPONENT_ID? If not then that's your problem. I got it to work and I'll post a demo in a couple of minutes.
-
Aug 16th, 2004, 10:27 AM
#8
Thread Starter
New Member
If you mean the Item and SubItem key, I don't fill them in, so I guess they don't have any value.
-
Aug 16th, 2004, 10:27 AM
#9
I can't do a demo because I've found out that lvwPartial doesn't apply to subitems, only the main item so I guess you'll need to code a loop.
-
Aug 16th, 2004, 10:37 AM
#10
Thread Starter
New Member
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?
-
Aug 16th, 2004, 10:42 AM
#11
VB Code:
Private Sub ListView1_KeyPress(KeyAscii As Integer)
KeyAscii = 0
End Sub
-
Aug 16th, 2004, 10:53 AM
#12
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|