PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Finding Partial Text Within A ListBox-VBForums
Results 1 to 19 of 19

Thread: [RESOLVED] Finding Partial Text Within A ListBox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Resolved [RESOLVED] Finding Partial Text Within A ListBox

    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.

    There is no other forum like VBFORUMS.

  2. #2
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,989

    Re: Finding Partial Text Within A ListBox

    Hmm Let me check for "r" and then "ru"...

    the following code will take to directly to list1 if there is a match

    VB Code:
    1. Private Sub Text1_Change()
    2.     List1.Text = Text1.Text
    3. End Sub

    for example if list1 has "asdfgh" and if you type "asdfgh" in text1 then "asdfgh" in list1 will get highlighted...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - 2015 IMP Links : Acceptable Use Policy, FAQ
    MyGear:
    ACER R7 (Win 8.1+Office 2013+VS2013) || Sony VPCCB-45FN with a Win10+Office 2010. || Mac Book Pro (10.6.8) with Office 2011

  3. #3
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Finding Partial Text Within A ListBox

    VB Code:
    1. 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
    2. Const LB_FINDSTRING = &H18F
    3. Private Sub Text1_Change()
    4.     'Retrieve the item's listindex
    5.     List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
    6. End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Finding Partial Text Within A ListBox

    Quote Originally Posted by Andrew G
    VB Code:
    1. 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
    2. Const LB_FINDSTRING = &H18F
    3. Private Sub Text1_Change()
    4.     'Retrieve the item's listindex
    5.     List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
    6. End Sub
    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.

    There is no other forum like VBFORUMS.

  5. #5
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Finding Partial Text Within A ListBox

    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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Finding Partial Text Within A ListBox

    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.

    There is no other forum like VBFORUMS.

  7. #7
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Finding Partial Text Within A ListBox

    Quote Originally Posted by koolsid
    Hmm Let me check for "r" and then "ru"...

    the following code will take to directly to list1 if there is a match

    VB Code:
    1. Private Sub Text1_Change()
    2.     List1.Text = Text1.Text
    3. End Sub

    for example if list1 has "asdfgh" and if you type "asdfgh" in text1 then "asdfgh" in list1 will get highlighted...
    That's not what he's asking for.

  8. #8
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Finding Partial Text Within A ListBox

    Quote Originally Posted by GRPsuper9
    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

  9. #9
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Finding Partial Text Within A ListBox

    Quote Originally Posted by Andrew G
    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 :\

  10. #10
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Finding Partial Text Within A ListBox

    Quote Originally Posted by GRPsuper9
    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.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Finding Partial Text Within A ListBox

    cuz ive seen many other programs, i forgot the names, but ive seen them do it, where they eliminate the things from the list, without lagging.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

  12. #12
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Finding Partial Text Within A ListBox

    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.

  13. #13
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,132

    Re: Finding Partial Text Within A ListBox

    I can't say for sure how it would work for a huge list but maybe something like this will work for you.
    VB Code:
    1. Private Sub Form_Load()
    2.     List1.AddItem "Peaches"
    3.     List1.AddItem "Nectarines"
    4.     List1.AddItem "Pears"
    5.     List1.AddItem "Quinces"
    6.     List1.AddItem "Cherries"
    7.     List1.AddItem "Apricots"
    8.     List1.AddItem "Apples"
    9.     List1.AddItem "Plums"
    10.     List1.AddItem "Figs"
    11.     List1.AddItem "Kiwis"
    12.     List1.AddItem "Olives"
    13.     List1.AddItem "Clementine"
    14.     List1.AddItem "Kumquat"
    15.     List1.AddItem "Minneola"
    16.     List1.AddItem "Mandarin"
    17.     List1.AddItem "Orange"
    18.     List1.AddItem "Satsuma"
    19.     List1.AddItem "Tangarine"
    20.     List1.AddItem "Tangelo"
    21.     List1.AddItem "Lemon"
    22.     List1.AddItem "Rough Lemon"
    23.     List1.AddItem "Lime"
    24.     List1.AddItem "Leech Lime"
    25.     List1.AddItem "Grapefruit"
    26.     List1.AddItem "Pummelo"
    27.     List1.AddItem "Sweety"
    28.     List1.AddItem "Ugli"
    29. End Sub
    30.  
    31. Private Sub Text1_Change()
    32. Dim i As Integer
    33.  
    34.     For i = List1.ListCount - 1 To 0 Step -1
    35.         If UCase(Left(List1.List(i), Len(Text1.Text))) <> UCase(Text1.Text) Then
    36.             List1.RemoveItem i
    37.         End If
    38.     Next i
    39. End Sub

  14. #14
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Finding Partial Text Within A ListBox

    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:
    1. 'NOTE: Make sure Listbox is sorted
    2.  
    3. Option Explicit
    4.  
    5. 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
    6. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    7.  
    8. Const LB_FINDSTRING = &H18F
    9.  
    10. Dim ListBoxItems() As String
    11. Const Letters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    12. Dim LetterPos(1 To 26) As Long
    13.  
    14. Dim PrevTextLen As Long
    15.  
    16.  
    17. Private Sub Form_Load()
    18. Dim Itm As Long
    19. Dim CurrentLetter As String
    20. Dim PrevLetter As String
    21.  
    22.  
    23.     'Add Items
    24.     List1.AddItem "Peaches"
    25.     List1.AddItem "Nectarines"
    26.     List1.AddItem "Pears"
    27.     List1.AddItem "Quinces"
    28.     List1.AddItem "Cherries"
    29.     List1.AddItem "Apricots"
    30.     List1.AddItem "Apples"
    31.     List1.AddItem "Plums"
    32.     List1.AddItem "Figs"
    33.     List1.AddItem "Kiwis"
    34.     List1.AddItem "Olives"
    35.     List1.AddItem "Clementine"
    36.     List1.AddItem "Kumquat"
    37.     List1.AddItem "Minneola"
    38.     List1.AddItem "Mandarin"
    39.     List1.AddItem "Orange"
    40.     List1.AddItem "Satsuma"
    41.     List1.AddItem "Tangarine"
    42.     List1.AddItem "Tangelo"
    43.     List1.AddItem "Lemon"
    44.     List1.AddItem "Rough Lemon"
    45.     List1.AddItem "Lime"
    46.     List1.AddItem "Leech Lime"
    47.     List1.AddItem "Grapefruit"
    48.     List1.AddItem "Pummelo"
    49.     List1.AddItem "Sweety"
    50.     List1.AddItem "Ugli"
    51.  
    52.    
    53.     'We now add them all to an array. Since we sorted first, it makes it easier
    54.     ReDim ListBoxItems(1 To List1.ListCount) As String
    55.    
    56.     For Itm = 1 To List1.ListCount
    57.         CurrentLetter = Left(List1.List(Itm), 1)
    58.         If CurrentLetter <> PrevLetter And Len(CurrentLetter) > 0 Then
    59.             LetterPos(InStr(1, Letters, CurrentLetter, vbTextCompare)) = Itm
    60.             PrevLetter = CurrentLetter
    61.         End If
    62.         ListBoxItems(Itm) = List1.List(Itm - 1)
    63.     Next
    64. End Sub
    65.  
    66. Private Sub Text1_Change()
    67. Dim FirstLetter As String
    68. Dim UpperRange As Integer
    69. Dim LowerRange As Integer
    70. Dim Itm As Integer
    71. Dim x As Long
    72. Dim RemItem As Long
    73. Dim r As Long
    74.  
    75. LockWindowUpdate List1.hwnd
    76. If Len(Text1) = 0 Then      'There is nothing in the textbox so we have to write all items again
    77.  
    78.     List1.Clear
    79.     For Itm = 1 To UBound(ListBoxItems)
    80.         List1.AddItem ListBoxItems(Itm)
    81.     Next
    82.  
    83. ElseIf Len(Text1) < PrevTextLen Then
    84.     'We deleted something
    85.        
    86.     List1.Clear
    87.    
    88.     FirstLetter = Left(Text1, 1)
    89.    
    90.     'Get Range
    91.     LowerRange = LetterPos(InStr(1, Letters, FirstLetter, vbTextCompare))
    92.    
    93.     For x = InStr(1, Letters, FirstLetter, vbTextCompare) + 1 To 26 Step 1
    94.         If Not LetterPos(x) = 0 Then
    95.             UpperRange = LetterPos(x)
    96.             Exit For
    97.         End If
    98.     Next
    99.    
    100.     If LowerRange < 1 Or UpperRange < 1 Then
    101.         PrevTextLen = Len(Text1)
    102.         LockWindowUpdate 0
    103.         Exit Sub
    104.     End If
    105.    
    106.     For r = LowerRange To UpperRange
    107.         If InStr(1, ListBoxItems(r), Text1, vbTextCompare) = 1 Then
    108.             List1.AddItem ListBoxItems(r)
    109.         End If
    110.     Next
    111.  
    112.  
    113. ElseIf Len(Text1) = 1 Then
    114. 'First Letter, so we remove all unwanted items before and after it
    115. 'Should be a bit faster than having to check each item has the text or not
    116.  
    117.     FirstLetter = Left(Text1, 1)
    118.    
    119.     If LetterPos(InStr(1, Letters, FirstLetter, vbTextCompare)) < 1 Then
    120.         List1.Clear
    121.         PrevTextLen = Len(Text1)
    122.         LockWindowUpdate 0
    123.         Exit Sub
    124.     End If
    125.    
    126.     'Get Range
    127.     LowerRange = LetterPos(InStr(1, Letters, FirstLetter, vbTextCompare))
    128.         If LowerRange = 1 Then LowerRange = 0
    129.    
    130.     For x = InStr(1, Letters, FirstLetter, vbTextCompare) + 1 To 26 Step 1
    131.         If Not LetterPos(x) = 0 Then
    132.             UpperRange = LetterPos(x)
    133.             Exit For
    134.         End If
    135.     Next
    136.         If UpperRange = 0 Then UpperRange = List1.ListCount
    137.    
    138.     'Remove all those after it
    139.     For RemItem = UpperRange To List1.ListCount - 1 Step 1
    140.         List1.RemoveItem UpperRange
    141.     Next
    142.    
    143.     'Remove all those before it
    144.     For RemItem = 1 To LowerRange Step 1
    145.         List1.RemoveItem 0
    146.     Next
    147.    
    148.    
    149. ElseIf Len(Text1) > 1 Then
    150.     'Remove items using a loop now. Anything that doesn't match
    151.     For r = List1.ListCount - 1 To 0 Step -1
    152.         'When we look at the item string,
    153.         'we must make sure that the text we wrote starts at pos 1
    154.         If InStr(1, List1.List(r), Text1, vbTextCompare) <> 1 Then
    155.             List1.RemoveItem r
    156.         End If
    157.     Next
    158. End If
    159.  
    160. PrevTextLen = Len(Text1)
    161. LockWindowUpdate 0
    162.  
    163. 'Retrieve the item's listindex
    164. List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
    165. End Sub
    I also borrowed MarkT's list items
    Last edited by Andrew G; Jan 8th, 2007 at 02:11 AM.

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Finding Partial Text Within A ListBox

    here's my go.

    it uses three ListBoxes

    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:
    1. Private Sub Text1_Change()
    2.     Dim lAsc As Long, lStart As Long, lEnd As Long
    3.     Dim sText As String
    4.    
    5.     If Trim$(Text1.Text) = vbNullString Then lstFull.ZOrder: Exit Sub
    6.    
    7.     sText = UCase$(Trim$(Text1.Text))
    8.    
    9.     lAsc = Asc(sText)
    10.     lStart = lIndex(lAsc - 65)
    11.    
    12.     lstPart.Clear
    13.     If (lStart = 0) And Not (lAsc = 65) Then
    14.         lstPart.AddItem "No Matches"
    15.     Else
    16.         For lAsc = lAsc + 1 To 90
    17.             If lIndex(lAsc - 65) > 0 Then lEnd = lIndex(lAsc - 65) - 1: Exit For
    18.         Next
    19.         If lAsc > 90 Then lEnd = lstFull.ListCount - 1
    20.        
    21.         lStart = SendMessage(lstFull.hwnd, LB_FINDSTRING, lStart - 1, ByVal sText)
    22.         If lStart > -1 Then
    23.             For lAsc = lStart To lstRev.ListCount - SendMessage(lstRev.hwnd, LB_FINDSTRING, _
    24.                                                       lstRev.ListCount - lEnd - 2, ByVal sText) - 1
    25.                 lstPart.AddItem lstFull.List(lAsc)
    26.             Next lAsc
    27.         Else
    28.             lstPart.AddItem "No Matches"
    29.         End If
    30.     End If
    31.     lstPart.ZOrder
    32. End Sub
    it's instantaneous with 30000 items, and, more importantly, it never has to repopulate the full list.

    i've attached it so you can see what i mean about the three ListBoxes.

    Attached Files Attached Files

  16. #16
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Finding Partial Text Within A ListBox

    An awesome bit of code Bush , heaps faster than my try

  17. #17
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Finding Partial Text Within A ListBox

    Nice Bush it's fast! Good Job.

  18. #18
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [RESOLVED] Finding Partial Text Within A ListBox

    what is this program for?

  19. #19
    Junior Member
    Join Date
    Aug 2006
    Posts
    25

    Re: [RESOLVED] Finding Partial Text Within A ListBox

    Very nice but is there a way to limit to 1 listbox instead of 3? as it seems unprofessional that way
    Last edited by revolutionx; Apr 20th, 2007 at 06:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width