ListView Question

jdlc

Registered User.
Local time
Today, 15:18
Joined
Mar 26, 2013
Messages
56
I'm not sure if this was already taken up in here, here is my dilema:

i have a textbox and i have a listview in a form, what i want to accomplish is when ever i type a value in a textbox it will search in the listview and hightlight the item, the code that i have is this:


Code:
Private Sub Text12_Change()
 
FindItem Me.Text12.Text, 1
     
End Sub
 
Sub FindItem(strSearch As String, iSubItemIndex As Integer)
Dim i As Long
    
For i = 1 To Me.ListView6.ListItems.Count
    If Me.ListView6.ListItems(i).SubItems(iSubItemIndex) Like "*" &  strSearch & "*" Then 'you could also use the LIKE operator
        Me.ListView6.ListItems(i).Selected = True
        Me.ListView6.ListItems(i).EnsureVisible
        Me.ListView6.SetFocus
        Exit For
    End If
Next
 
End Sub

the problem with this code, after i put one character in the textbox it work it search and highlight but it doesn't return the focus in the textbox for to complete the item to be search.

thanks in advance for any solution you may have and i do appreaciate in any advice.
 
basically beacuse you are moving the focus to Listview6

try removing this line

thanks for the reply cj, but if i remove that line the item is not immediately highlighted not until i lost focus in the textbox.

what i'm trying to achieve is whenever i type a charcter in the textbox the listview will scroll and highlight the item.
 
Not sure you will get the effect you want, but if you want to return control to your text control you will need to return the focus e.g.

Code:
...
Me.ListView6.SetFocus
Me.text12.SetFocus

or you could try replacing

Me.ListView6.SetFocus

with

Me.ListView6.requery

if you lose focus from your text box and want to return to the same position in the text, then you'll also need additional code to a) store that position and then b) return to that position.

For this look at the SelLength property - here is a link to a thread which had a similar question

http://www.access-programmers.co.uk/forums/showthread.php?t=151884
 
The search can be performed in Key Up event in place of the Change event.

By the way I would imagine the data source of your listview is a table/query so you're probably better off performing the search on a recordset and using the AbsolutePosition to as the index of the listbox's Selected property.

If you have very few records then don't worry about using a recordset.
 
The search can be performed in Key Up event in place of the Change event.
True, but that doesn't stop the OP's code moving the focus to the listview box.
 
It should encourage it to move to the listview control because the user has stopped typing at this point.
 
thanks for the reply cj, vbinet i do appreciate your suggestion, it work well, the listview is scrolling to the item i'm searching, though it didn't highlight the searched item in the listview because of the textbox setfocus, one thing i can think off is to change the backcolor, is there a way to change the backcolor?
 
thanks for the reply cj, vbinet i do appreciate your suggestion, it work well, the listview is scrolling to the item i'm searching, though it didn't highlight the searched item in the listview because of the textbox setfocus, one thing i can think off is to change the backcolor, is there a way to change the backcolor?
What worked well?
 
is there a way to change the backcolor?
Don't think so, I don't use the control myself so can;t help with that. Suggest you search online for articles about the access listview control
 
Don't think so, I don't use the control myself so can;t help with that. Suggest you search online for articles about the access listview control
Apparently it can only be done through APIs. I tried subclassing it once but I didn't finish.
 
@vbainet, it work well when i put it in the key up event. though as i mentioned the highlighting in listview part is gone, well technically it highlighted but since i'm putting the focus back to the textbox you can't see it anymore.

@cj, well this is funny because the place i'm working still using the xp as os the ms verion is 2003. anyways i can use the link for the future use.

again many thanks brothers, i guess i need to close this case but if you guys pick up something, please let me know. i will try to look and will share it here (good luck for me).
 
@vbainet, it work well when i put it in the key up event. though as i mentioned the highlighting in listview part is gone, well technically it highlighted but since i'm putting the focus back to the textbox you can't see it anymore.
What I was saying is use your original code in the Key Up event and see if it gives a different behaviour.
 
What I was saying is use your original code in the Key Up event and see if it gives a different behaviour.

hi boss, i did put the original code to the key up event, and i guess there's not much difference. here's the code:

Code:
Private Sub Text12_KeyUp(KeyCode As Integer, Shift As Integer)
 
FindItem Me.Text12.Text, 1
 
end sub
 
Sub FindItem(strSearch As String, iSubItemIndex As Integer)
 
Dim i As Long
For i = 1 To Me.ListView6.ListItems.Count
        If Mid(Me.ListView6.ListItems(i).SubItems(iSubItemIndex), 1, Trim(Len(strSearch))) = strSearch Then
            Me.ListView6.ListItems(i).EnsureVisible
            Me.ListView6.ListItems(i).Selected = True
            Me.ListView6.SetFocus
            Me.Label25.Caption = Me.ListView6.ListItems(i)
            Exit For
        End If
Next
 
[COLOR=red]' this is the line which caused the highlighted item gone[/COLOR]
[COLOR=red]Me.Text12.SetFocus[/COLOR]
[COLOR=red]pfPositionCursor Me.Text12, Len(Me.Text12 & "")[/COLOR]
 
End Sub
 
Public Function pfPositionCursor(ctl As Control, lngWhere As Long)
 
Select Case ctl.ControlType
Case AcControlType.acTextBox, AcControlType.acComboBox
    ctl.SelStart = lngWhere
    ctl.SelLength = 0
Case Else
    'Do Nothing
End Select
 
End Function
 
Hi master, your original code doesn't have the "Me.Text12.SetFocus", so try it without.
 
hi vbainet, there's no question without that line it will show the highlighted item in the listview but the problem is i need to put back the focus in the textbox.

example my search string is "Lee"
in the textbox, ill press the letter "L", listview will scoll and highlight the first "L" the he find, since the focus is now in the listview, i need to manually put the cursor back to the textbox and when i press another letter same thing will happen.

hoping you didnt get offended when i call you boss, it's just an expression to me when i'm talking to somebody. :)
 
I see! I'm putting together an example now.

No, I can't get offended by that. I called you master as a joke ;)
 
I see! I'm putting together an example now.

No, I can't get offended by that. I called you master as a joke ;)

thanks a lot for spending some of your precious time in this thread i do appreciate.
 

Users who are viewing this thread

Back
Top Bottom