Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search a listbox using a text box 1

Status
Not open for further replies.

ambra19

Programmer
Aug 5, 2002
32
0
0
CA
I want to add a search functionality to my program.
I have a listbox (which contains names, format: last name, first name) and want to put a textbox above it that searches the listbox in the following manner:
for example, i am looking for the last name "Smith"
when i type the letter "s" into the text box, it will go to the beginning of all of the "s" last names in the list box. then if i add an "m" so it reads "sm" it will bring the user to beginning of all of the last names that begin with "Sm"
If there is a posting on this, I apologize, but the keyword search on this site is not working.
Thanks.
ambra19
 
'Add followings to a Module file (.bas)
'You can use the same code for combo box also.
'Just have to use different constant and change the
'type in the AutoSelectItem argument from ListBox to ComboBox


'this constant is used by list box
Public Const LB_FINDSTRING = &H18F

'this constant is used by combo box
Public Const CB_FINDSTRING = &H14C

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lparam As Any) As Long

'Add a public sub to the same Module file (.bas file).
Public Sub AutoSelectItem(lstAny As ListBox)

Dim intStart As Integer
Dim strTemp As String
Static intLeftOff As Integer

intStart = 1
intStart = lstAny.SelStart

If intLeftOff <> 0 Then
lstAny.SelStart = intLeftOff
intStart = intLeftOff
End If

strTemp = CStr(Left$(lstAny.Text, intStart))
lstAny.ListIndex = SendMessage(lstAny.hwnd, LB_FINDSTRING, -1, _
ByVal CStr(Left$(lstAny.Text, intStart)))

If lstAny.ListIndex = -1 Then
intLeftOff = Len(strTemp)
lstAny.Text = strTemp
End If

lstAny.SelStart = intStart
intLeftOff = 0

End Sub


'Now add following code to the change event of the text box control
Private Sub Text1_Change ()

AutoSelectItem List1

End Sub
 
I tried this, but it I got a &quot;Compile Error: Method or data not Found&quot;
How can I fix this?
thanks.
ambra19
 
'Replace the AutoSelectItem in the .bas file and Text1_Change() with the following.

'Add a public sub to the same Module file (.bas file).
Public Sub AutoSelectItem(lstAny As ListBox, txtAny As TextBox)

Dim intStart As Integer
Dim strTemp As String
Static intLeftOff As Integer

intStart = 1
intStart = txtAny.SelStart

If intLeftOff <> 0 Then
txtAny.SelStart = intLeftOff
intStart = intLeftOff
End If

strTemp = CStr(Left$(txtAny.Text, intStart))
lstAny.ListIndex = SendMessage(lstAny.hwnd, LB_FINDSTRING, -1, _
ByVal CStr(Left$(txtAny.Text, intStart)))

If lstAny.ListIndex = -1 Then
intLeftOff = Len(strTemp)
lstAny.Text = strTemp
End If

txtAny.SelStart = intStart
intLeftOff = 0

End Sub

'Now add following code to the change event of the text box control
Private Sub Text1_Change()

AutoSelectItem List1, Text1

End Sub
 
jollypathak,

thank you very much, it works perfectly.
ambra19
 
jollypathak,

I just encountered a problem, when I type in the first letters ie 't', 'ti' then change to 'tu' it works, but when i get rid of all of the letters in order to go to the 's' for example, i get the following error:
Runtime error 318
Invalid property array index

because the the id associated with the listbox is now 0.
I tried an IF statement where if the id was 0 it would exit the sub, but that didn't work.
How can i fix this?

Thank you so much for all of your help thus far.
Regards,
ambra19
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top