lets say i have a textbox and a listbox, Text1 and List1. List 1 is filled with many words. and what i want to do is, whenever the user starts typing in Text1, it starts taking him to the place in List1. for instance
user types: "r", it would take him to the first R in the list. then the user entered another letter, so now the user has the following
user has: "ru", so now the listbox is in the "ru" place, then he types "n" and it takes him to the place where all the run's are.
i guess kind of like how you press F1 in VB6, and you use its index search. or something like that. how would i do this?
if u need clarifications, just ask.
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
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
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
exactly what i wanted . thanks a ton. but now, this is a totally diff question, when the user is searching for those things, how would i eliminate (basicly remove from listbox) the items in the listbox that dont have anything in commen with what the user is searching? and then if the user presses backspace, it shows him his possibilitys. heres an example (btw, this does have 2 do with my question, its not different)
exp:
List1 items:
A
Allie
Cool
Sweet
now, lets say the user typed "A", it would remove cool and sweet, now if the user typed "al", it would remove everything that doesn't have "al" in it. then, if the user presses backspace and goes back to have "A", i want the "A" and "Allie" to both be showing, and if he has nothing in his textbox, then all items show again. if u need clarification, again, ask.
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
The only way i can think of to do something like this, is to have all the items in an array, then loop all the available items, and only add the ones that match each time. But with a lot of items, and the possibility of having the listbox keep refreshing as items are added, it could slow down the program. Maybe there is an easier way to what you want to achieve.
ya, an array is the way i would do it too. there are 1355 items in it. and the fact that it would slow down the program, makes sense. if anyone knows how 2 do this an easier way that doesn't slow down the program as much, post .
If ive helped, RATE my post.
If your thread is solved, and you got your answer, mark this thread Resolved.
exactly what i wanted . thanks a ton. but now, this is a totally diff question, when the user is searching for those things, how would i eliminate (basicly remove from listbox) the items in the listbox that dont have anything in commen with what the user is searching? and then if the user presses backspace, it shows him his possibilitys. heres an example (btw, this does have 2 do with my question, its not different)
exp:
List1 items:
A
Allie
Cool
Sweet
now, lets say the user typed "A", it would remove cool and sweet, now if the user typed "al", it would remove everything that doesn't have "al" in it. then, if the user presses backspace and goes back to have "A", i want the "A" and "Allie" to both be showing, and if he has nothing in his textbox, then all items show again. if u need clarification, again, ask.
you would need to, when you add the items to a list, create a string of all the items separated by a common dilimiter. Then, every time something is typed in the textbox, split the saved string by the delimiter and scan through it to see if any of them start with what is contained in the text box and add them to the list. Upon changing the textbox(be it adding or deleting a character), you'd have to remember to clear the list before scanning the string and adding the matches ;D
The only way i can think of to do something like this, is to have all the items in an array, then loop all the available items, and only add the ones that match each time. But with a lot of items, and the possibility of having the listbox keep refreshing as items are added, it could slow down the program. Maybe there is an easier way to what you want to achieve.
****. Wish I would have ready this prior to my last post :\
ya, an array is the way i would do it too. there are 1355 items in it. and the fact that it would slow down the program, makes sense. if anyone knows how 2 do this an easier way that doesn't slow down the program as much, post .
That's the only way I think you could do it, really.
If the item array is sorted in alphabetical order, it might improve the speed as you don't need to sort through every item. Once you can't find any more matches then you wouldn't need to continue searching the array. Also if the items are all known, you could further improve the speed by making it search for matches from known positions, for example if you know that all entries starting with a 'T' start at item 400 then you could search from 400 to 500 (if thats for 'U'), hence you don't need to go through the first 400 and the ones after 500.
This may still have heaps of bugs etc, but it should be close to what you need.
Edit: Tested it with a large listbox but was too slow. It didn't refresh often, but it was still too slow
Code:
VB Code:
'NOTE: Make sure Listbox is sorted
Option Explicit
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
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Const LB_FINDSTRING = &H18F
Dim ListBoxItems() As String
Const Letters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim LetterPos(1 To 26) As Long
Dim PrevTextLen As Long
Private Sub Form_Load()
Dim Itm As Long
Dim CurrentLetter As String
Dim PrevLetter As String
'Add Items
List1.AddItem "Peaches"
List1.AddItem "Nectarines"
List1.AddItem "Pears"
List1.AddItem "Quinces"
List1.AddItem "Cherries"
List1.AddItem "Apricots"
List1.AddItem "Apples"
List1.AddItem "Plums"
List1.AddItem "Figs"
List1.AddItem "Kiwis"
List1.AddItem "Olives"
List1.AddItem "Clementine"
List1.AddItem "Kumquat"
List1.AddItem "Minneola"
List1.AddItem "Mandarin"
List1.AddItem "Orange"
List1.AddItem "Satsuma"
List1.AddItem "Tangarine"
List1.AddItem "Tangelo"
List1.AddItem "Lemon"
List1.AddItem "Rough Lemon"
List1.AddItem "Lime"
List1.AddItem "Leech Lime"
List1.AddItem "Grapefruit"
List1.AddItem "Pummelo"
List1.AddItem "Sweety"
List1.AddItem "Ugli"
'We now add them all to an array. Since we sorted first, it makes it easier
ReDim ListBoxItems(1 To List1.ListCount) As String
For Itm = 1 To List1.ListCount
CurrentLetter = Left(List1.List(Itm), 1)
If CurrentLetter <> PrevLetter And Len(CurrentLetter) > 0 Then
lstFull - Sorted = True, contains full list
lstPart - used to show the matches - in exactly the same position as lstFull
lstRev - Visible = False, the full list reversed.
and this is the search code:
VB Code:
Private Sub Text1_Change()
Dim lAsc As Long, lStart As Long, lEnd As Long
Dim sText As String
If Trim$(Text1.Text) = vbNullString Then lstFull.ZOrder: Exit Sub
sText = UCase$(Trim$(Text1.Text))
lAsc = Asc(sText)
lStart = lIndex(lAsc - 65)
lstPart.Clear
If (lStart = 0) And Not (lAsc = 65) Then
lstPart.AddItem "No Matches"
Else
For lAsc = lAsc + 1 To 90
If lIndex(lAsc - 65) > 0 Then lEnd = lIndex(lAsc - 65) - 1: Exit For