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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List box Touble

Status
Not open for further replies.

goatsaregreat

Programmer
Mar 21, 2001
82
GB
I have a list box that is populated from a DB when the form loads. The list box is not connected to a data access object. The list box contains a list of equipment owned by a rental joint. I need to be able to type in the first letter of a word, and have the list box jump to the first entry of that letter. When I type a second letter, I want the list box to react by showing the first entry starting with those two letters. And so on. I have seen this in action hundreds of times, but do not know the code to use. TIA for your quick responses. If you need anymore info, let me know.
 
A simple way...

Create a TextBox on the form (i'll call it as Text1)

Code:
Private Sub List1_KeyPress(KeyAscii As Integer)
    Text1.Text = Chr$(KeyAscii)
    Text1.SetFocus
    Text1.SelStart = Len(Text1.Text)
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    For i = 0 To List1.ListCount - 1
         If LCase(Left$(List1.List(i), Len(Text1.Text))) = LCase(Text1.Text) Then
              List1.ListIndex = i
              Exit Sub
         End If
    Next i
    List1.ListIndex = -1
End Sub

It will highlite the listbox entry that matches with your criteria. If nothing matches it'll highlite none...

Hope it works.
Pls let me know if it works or not...
 
Hi,

A MUCH faster and better way would be using this code:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Const LB_FINDSTRING = &H18F

Private Sub Form_Load()
For I% = 1 To 20000
List1.AddItem CStr(I%)
List2.AddItem CStr(I%)
Next I%

List1.AddItem "SendMessage"
List2.AddItem "For-Next Loop"
End Sub

Private Sub Text1_Change()
Dim sBuffer As String
Dim Ret As Long
Dim T As Long

T = GetTickCount

sBuffer = Text1.Text

If Len(sBuffer) = 0 Then
List1.ListIndex = 0
Else
Ret = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal sBuffer)
If Ret = -1 Then
List1.ListIndex = 0
Else
List1.ListIndex = Ret
End If
End If

MsgBox "It took Visual Basic " & GetTickCount - T & "milliseconds to find your result using SendMessage"
End Sub

Private Sub Text2_Change()
Dim T As Long

T = GetTickCount

For I% = 0 To List2.ListCount - 1
If LCase(Left$(List2.List(I%), Len(Text2.Text))) = LCase(Text2.Text) Then
List2.ListIndex = I%
Exit Sub
End If
Next I%
List2.ListIndex = -1

MsgBox "It took Visual Basic " & GetTickCount - T & "milliseconds to find your result using a For-Next Loop"
End Sub

Here you can compare the way he told you and the way using API. See the difference ;-)

LuCkY
 
Ow, made a slight mistake on his function, it should be like this:

Private Sub Text2_Change()
Dim T As Long

T = GetTickCount

For I% = 0 To List2.ListCount - 1
If LCase(Left$(List2.List(I%), Len(Text2.Text))) = LCase(Text2.Text) Then
List2.ListIndex = I%
MsgBox "It took Visual Basic " & GetTickCount - T & "milliseconds to find your result using a For-Next Loop"
Exit Sub
End If
Next I%
List2.ListIndex = -1

MsgBox "It took Visual Basic " & GetTickCount - T & "milliseconds to find your result using a For-Next Loop"
End Sub

 
LuckyLuke's method is indeed much faster - and can be implemented for your requirements in a single line of code. For the purposes of this example you need a form with your listbox and a textbox on it (as per aliinal's example). Then add the following code:

Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_FINDSTRING = &H18F


Private Sub Text1_Change()
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal Text1.Text)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top