Click to See Complete Forum and Search --> : Listbox and textbox


Raptors Fan
September 6th, 2001, 12:12 PM
Hi,

I can't figure out how to used a textbox and a listbox where the user types in the textbox and this causes an item to be highlighted in the listbox. This is often used in Help Searches. Basically, instead of scrolling through the listbox to find an item, you search for it by typing the first letters of the item, which is then highlighted on the listbox.

Can someone give me an idea on how this works or some code that points me to the right direction.

Much appreciated.

RF

PS I don't needed for help searches, but to make it easier for the user to find the list item.

Andrew R.
September 6th, 2001, 01:37 PM
Shouldn't be that hard.
In text-box trap 'Change' event.
Then search for typed value in the list-box.
Select found item in the list-box.
When you loop through the list-box items do something like this:

if value = Left(listboxitem, len(value)) then
' select this item and exit loop
end if

Iouri
September 6th, 2001, 02:03 PM
Here is autocomplete word in combo. The same works for listbox

Declare Function SendMessage Lib ""User32"" Alias _
""SendMessageA"" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long

Const CB_ERR = -1
Const CB_FINDSTRING = &H14C


'Add the sub

Sub sMatchEntry(cbo As ComboBox, KeyAscii As Integer)
Dim sBuffer As String
Dim lRetVal As Long

sBuffer = Left(cbo.Text, cbo.SelStart) & Chr(KeyAscii)
lRetVal = SendMessage((cbo.hWnd), CB_FINDSTRING, _
-1, ByVal sBuffer)
If lRetVal <> CB_ERR Then
With cbo
.ListIndex = lRetVal
.Text = .List(lRetVal)
.SelStart = Len(sBuffer)
.SelLength = Len(.Text)
End With
KeyAscii = 0
End If
End Sub

' in the KeyPress Event of the Combobox, add:

Private Sub Combo1_KeyPress(KeyAscii As Integer)
sMatchEntry Combo1, KeyAscii
End Sub"





Iouri Boutchkine
iouri@hotsheet.com

Raptors Fan
September 7th, 2001, 02:44 PM
Thanks,

I used your method for comboboxes and works like a charm! ;)

However, it doesn't work with listboxes. It seems listboxes don't have the .selstart and the .sellength properties.

What I have is a textbox and a listbox and I have used the keypress event in the textbox. The textbox does have the .selstart and .sellength methods. I have played around with the code but I can't seem to get it to work for the listbox. Can you see if you have better luck than me.

Much appreciated,

RF

michi
September 7th, 2001, 03:59 PM
I have a function created for ListView, I guess you can change it for ListBox.

====
Public Sub MakeEntryInListViewVisible(ByVal strTextToFind As String, _
ByRef lsvTheListView As MSComctlLib.ListView, _
Optional ByVal blnCaseSensitive As Boolean = False, _
Optional ByVal blnReraiseErrors As Boolean = False)
On Error GoTo MakeEntryInListViewVisible_Error


Dim intCount As Integer ' Loop Counter
Dim blnFound As Boolean ' Indicates if the entry to locate is found
Dim intColumn As Integer ' Identifes the column in the listview to search on.
Dim intItemToFocus As Integer ' idenfies the entry to make visible
Dim intStartIndex As Integer ' The starting point to search in the listview
Dim intEndIndex As Integer ' The ending point to search in the listview
Dim intStep As Integer ' The step for the search either 1 or -1.
' Forward or backward.

If (lsvTheListView.Sorted = True And lsvTheListView.Visible = True) Then

'
' If we do not find an entry we must go to the start or end
' of the list depending on the sort order.
'
If (lsvTheListView.SortOrder = lvwAscending) Then
intItemToFocus = lsvTheListView.ListItems.Count
Else
intItemToFocus = 1
End If

If (strTextToFind = "") Then
'
' Always go to the top of the list if the search criteria
' is blank.
'
intItemToFocus = 1
Else
'
' If the listview is sorted ascending, search from the top to the bottom.
' If the listview is sorted descending, search from the bottom to the top.
'
If (lsvTheListView.SortOrder = lvwAscending) Then
intStartIndex = 1
intEndIndex = lsvTheListView.ListItems.Count
intStep = 1
Else
intStartIndex = lsvTheListView.ListItems.Count
intEndIndex = 1
intStep = -1
End If

If (lsvTheListView.SortKey = 0) Then
'
' Do the search based on the first column of the listview. This means
' comparing the text property of the listview listitems.
'
For intCount = intStartIndex To intEndIndex Step intStep
If (blnCaseSensitive = True) Then
If (lsvTheListView.ListItems(intCount).Text > strTextToFind) Then
intItemToFocus = intCount
Exit For
End If
Else
'
' Since we are not case sensitive, compare using all uppercase letters.
'
If (UCase(lsvTheListView.ListItems(intCount).Text) > UCase(strTextToFind)) Then
intItemToFocus = intCount
Exit For
End If
End If
Next intCount
Else
'
' Do the search based on the sub columns of the listview. This means comparing
' the text property of the listsubitem collection of the listitems in the listview.
' The sortkey defines the collection index of the listsubitems to search upon.
'
intColumn = lsvTheListView.SortKey
For intCount = intStartIndex To intEndIndex Step intStep
If (blnCaseSensitive = True) Then
If (lsvTheListView.ListItems(intCount).ListSubItems(intColumn).Text > strTextToFind) Then
intItemToFocus = intCount
Exit For
End If
Else
'
' Since we are not case sensitive, compare using all uppercase letters.
'
If (UCase(lsvTheListView.ListItems(intCount).ListSubItems(intColumn).Text) > UCase(strTextToFind)) Then
intItemToFocus = intCount
Exit For
End If
End If
Next intCount
End If
End If

If (intItemToFocus > 0) Then
'
' We found an item so make it visible.
'
lsvTheListView.ListItems(intItemToFocus).EnsureVisible
End If
End If

Exit Sub

MakeEntryInListViewVisible_Error:

'
' If requested reraise the error to the calling module.
'
If (blnReraiseErrors = True) Then
Call Err.Raise(Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext)
End If

End Sub

'
'Then in the Form
'
Private Sub txtSearch_Change()
Call MakeEntryInListViewVisible(txtSearch.Text, lsvBillingNames, False, True)

End Sub


======

Hope this helps.


Regards,

Michi

DSJ
September 7th, 2001, 04:31 PM
Give this a try...


option Explicit
private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, byval lParam as string) as Long
private Const LB_FINDSTRING = &H18F
private Const LB_FINDSTRINGEXACT = &H1A2

private Sub Form_Load()
List1.AddItem "One"
List1.AddItem "Two"
List1.AddItem "Three"
List1.AddItem "Four"
End Sub

private Sub Text1_Change()
Dim l as Long
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, Text1.Text)
End Sub

Raptors Fan
September 7th, 2001, 04:41 PM
Cool! You got it to work...


Thanks a lot all you GURUS!!!! ;-)

sanket
July 22nd, 2002, 08:08 AM
Thanks everybody
Yourr advices helped me solve my problem within five minutes.
thanks a lot
Bye
Sanket