[RESOLVED] Textbox plus Listview Autocomplete
Hi everyone. I have an autocomplete textbox, which gets the data from the recordset. But the problem is I can't combine the listview and textbox.
I have seen some programs and I'm wondering what API call should I use.
Look at this example. This is the API Guide from www.allapi.net.
http://i16.photobucket.com/albums/b4...tocomplete.jpg
As you can see, when the user typed a letter, the textbox highlightened as well as the listview.
Any help will be greatly appreciated. :)
Re: Textbox plus Listview Autocomplete
maybe there is a better way or api to do what you want, but this woks for me
VB Code:
Dim i As Integer, bfound As Boolean
For i = 1 To lv1.ListItems.Count
lv1.ListItems(i).Selected = False
If Not bfound Then
If LCase(Left(lv1.ListItems(i), Len(tdate(Index)))) = tdate(Index) Then
lv1.ListItems(i).Selected = True
lv1.ListItems(i).EnsureVisible
bfound = True
End If
End If
Next
i put the boolean in as the listview was multi select, put the code in the textbox change event, change the names of you textbox and listview
Re: Textbox plus Listview Autocomplete
What's that tdate ?
I put the code after I call this procedure. Sort of filter.
VB Code:
Public Sub LookUp(SearchKey$)
Screen.MousePointer = vbHourglass
frmScribeix.StatusBar1.Panels(1).Text = "Searching!!!"
SearchKey$ = Trim(SearchKey$)
rssearch.Filter = "Name like '" & SearchKey$ & "%'"
If rssearch.RecordCount = 0 Then
ctr = 0
frmScribeix.lvScribe.ListItems.Clear
frmScribeix.StatusBar1.Panels(1).Text = "No record found for " & SearchKey$
Screen.MousePointer = vbNormal
Exit Sub
ElseIf rssearch.RecordCount > 0 Then
frmScribeix.lvScribe.ListItems.Clear
rssearch.MoveFirst
ctr = 0
Do Until rssearch.EOF
ctr = ctr + 1
lvScribe.ListItems.Add , , rssearch!Name
rssearch.MoveNext
Loop
Screen.MousePointer = vbNormal
Else
End If
frmScribeix.StatusBar1.Panels(1).Text = " " & ctr & " " & "records found"
End Sub
It didn't work either.
Re: Textbox plus Listview Autocomplete
tdate is textbox, (actually an array of text boxes, that's why it has an index),
any textbox will do, just change the name and remove the index part
it just compares the text in the textbox to the items in the listview and selects the first it finds that matches, unselects any others that may have been selected previously, in case of multiple select being enabled (as it is in my project)
Re: Textbox plus Listview Autocomplete
still no success. i'm having a conflict with my query. dunno where to put that piece of code.
Re: Textbox plus Listview Autocomplete
Quote:
put the code in the textbox change event, change the names of you textbox and listview
if you put the code in the textbox change event, it will work for typing, but also pasting
i tested the code and it worked ok
Re: Textbox plus Listview Autocomplete
Quote:
Originally Posted by westconn1
if you put the code in the textbox change event, it will work for typing, but also pasting
i tested the code and it worked ok
I also tested your code and it worked just fine.
zynder: Did you use westconn1's examples? In what event are you running the code from?
Re: Textbox plus Listview Autocomplete
I put the code in the text change event. But as I've said earlier I have a subroutine called on the text change event, which trigger the filtering of the recordset.
It's working fine I just want to add the autocomplete for the textbox.
VB Code:
Private Sub text1_Change()
Dim i As Integer, bfound As Boolean
' my filter
LookUp text1
'westconn1's code
For i = 1 To lv1.ListItems.Count
lv1.ListItems(i).Selected = False
If Not bfound Then
If LCase(Left(lv1.ListItems(i), Len(text1))) = text1 Then
lv1.ListItems(i).Selected = True
lv1.ListItems(i).EnsureVisible
bfound = True
End If
End If
Next
End Sub
There must be a conflict somewhere. :( Sorry guys i'm a noob.
Re: Textbox plus Listview Autocomplete
This is another way to do it. Its from API guide with some additions... If you want the full code its under "SendMessage", the first example.
the text box is Text1, Listbox is List1
VB Code:
'This project needs a ListBox, named List1 and a TextBox, named Text1
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Const LB_FINDSTRING = &H18F
Private Sub Text1_Change()
'Retrieve the item's listindex
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
'If something was chosen then we get the rest of it
If List1.ListIndex <> -1 Then
strt = Len(Text1.Text)
Text1.Text = List1.List(List1.ListIndex)
Text1.SelStart = strt
Text1.SelLength = Len(Text1.Text) - strt
End If
End Sub
Re: Textbox plus Listview Autocomplete
Re: Textbox plus Listview Autocomplete
Oops sorry.. i should have read it more carefully :o
Re: Textbox plus Listview Autocomplete
Ok this time its a listview ;)
VB Code:
Private Sub Text1_Change()
On Error GoTo ErrMsg
Dim LstItm As ListItem
Dim Strt as Integer
ListView1.FindItem(Text1.Text, , , 1).Selected = True
'If something was chosen then we get the rest of it
strt = Len(Text1.Text)
Text1.Text = ListView1.SelectedItem.Text
Text1.SelStart = strt
Text1.SelLength = Len(Text1.Text) - strt
Exit Sub
ErrMsg:
ListView1.SelectedItem.Selected = False
End Sub
Re: Textbox plus Listview Autocomplete
ok the listbox code works perfect and same with the listview but I'm having trouble with backspace or delete keys.
Maybe I should switch to listbox and try that API call. Thanks very much for the help. :)
Re: Textbox plus Listview Autocomplete
VB Code:
Dim DelKey As Boolean
Private Sub Text1_Change()
On Error GoTo ErrMsg
Dim LstItm As ListItem
ListView1.FindItem(Text1.Text, , , 1).Selected = True
If Not DelKey Then
'If something was chosen then we get the rest of it
strt = Len(Text1.Text)
Text1.Text = ListView1.SelectedItem.Text
Text1.SelStart = strt
Text1.SelLength = Len(Text1.Text) - strt
End If
DelKey = False
Exit Sub
ErrMsg:
ListView1.SelectedItem.Selected = False
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Or KeyCode = 8 Then DelKey = True
End Sub
Re: Textbox plus Listview Autocomplete
del key still ain't working as well as backspace. And the listview is not highlighted. I want it to be exactly the same as the picture above.
1 Attachment(s)
Re: Textbox plus Listview Autocomplete
Mmm... It should work fine...
I've attached the whole example anyway in case i might have missed something.
Also i should note that it only searches the first column, so it won't check for the other subitems. If You want it to check them as well, then your gonna have to change the FindItem function.
Re: Textbox plus Listview Autocomplete
Ok thanks once again. I'm really stressed out lately. I've been doing programming for the past two months straight and now I'm braindead. Maybe I should take a break.
Anyway thanks Andrew! :)
---edit-----
My Bad. HideSelection was checked!!! That's IT.
Re: [RESOLVED] Textbox plus Listview Autocomplete
i see it is a lot easier to do stuff in the list box than the way i was doing it, but i can't get you code to work right for filling the textbox and doing the selected text, this is because the sub is called recursivly when the text is changed within the sub and even when the the text is not changed within it, the sub runs twice if it overwrites selected text
Re: [RESOLVED] Textbox plus Listview Autocomplete
Exactly! Hey Westconn1, your code actually works now that I unchecked the HideSelection.
LOL. I wasn't paying attention to the properties so I ended up changing some of the codes, which I doubted to be causing the problem. It's okay I have my backup.
:wave: