Results 1 to 11 of 11

Thread: Listbox Search!?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    249

    Unhappy Listbox Search!?

    ok, how do i make it so that i have a listbox and a textbox and my listbox is filled with names (Anthony, Alex, Brad, Anthony, Anthony) and when i type a name in the textbox, it will only show that name in the listbox and will remove the rest of the other names. ex: If i type Anthony in the textbox then in the listbox all that will show is Anthony
    Anthony
    Anthony
    and Brad and Alex will not show. Does anyone know how to do this its a search method its in some software programs....please help! Thank you.

  2. #2
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Listbox Search!?

    Try doing a search for "Autocomplete"

    I found one of my old threads that worked here

    This will get you started.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Listbox Search!?

    Quote Originally Posted by aikidokid
    Try doing a search for "Autocomplete"

    I found one of my old threads that worked here

    This will get you started.
    Your link is for a ListView. The OP is asking about a ListBox, for which I would use something like
    vb Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    2. ByVal wMsg As Long, ByVal wParam As Long, lparam As Any) As Long
    3.  
    4. Private Const LB_FINDSTRING = &H18F  
    5. Private Const LB_FINDSTRINGEXACT = &H1A2
    6.  
    7. Private Sub txtSearch_Change()
    8. List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1,  ByVal CStr(Text1.Text))
    9. End Sub
    Last edited by Hack; Nov 19th, 2007 at 08:00 AM.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listbox Search!?

    To make it autocomplete you will want to use the Change event of the textbox perhaps instead of a command button.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Listbox Search!?

    Good point.

    Changed.

  6. #6
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Listbox Search!?

    Quote Originally Posted by Hack
    Your link is for a ListView.
    yeah the original question was about a Listview, but post 13 from zyander was for a Listbox
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Listbox Search!?

    Post 13?????

    There are only 6, now 7, posts in this thread.

  8. #8
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Listbox Search!?

    Quote Originally Posted by Hack
    Post 13?????

    There are only 6, now 7, posts in this thread.
    No - the thirteenth post in the link I gave.

    Quote Originally Posted by zynder
    Here is autocomplete for listbox.

    Code:
    Option Explicit
    'API call to listbox
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any _
    ) As Long
    Const LB_FINDSTRING = &H18F
    Dim DelKey As Boolean
    Dim bNoClick As Boolean
    
    Private Sub List1_Click()
    'to prevent executing the click event
     If bNoClick Then Exit Sub
        Text1.Text = List1.Text
    End Sub
    
    Private Sub List1_GotFocus()
        SendKeys "{DOWN}"
    End Sub
    
    Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
        List1_Click
    End Sub
    
    Private Sub Text1_Change()
    'autocomplete feature
    Dim strt As Long, nIndex As Long
    Dim nLen As Long, sText As String
    Const LB_GETTEXTLEN As Long = &H18A
    Const LB_GETTEXT As Long = &H189
    Static blnBusy As Boolean
    
    If blnBusy Then
       Exit Sub
    End If
         
         bNoClick = True
         blnBusy = True
        
        'Retrieve the item's listindex
        List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
        
        If Not DelKey Then
    
    
        If List1.ListIndex <> -1 Then
            strt = Len(Text1.Text)
            Text1.Text = List1.List(List1.ListIndex)
            Text1.SelStart = strt
            Text1.SelLength = Len(Text1.Text) - strt
        Else
        
        End If
        End If
           DelKey = False
           blnBusy = False
           bNoClick = False
    
    End Sub
    
    Private Sub Text1_Click()
        'select all the text
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1)
    End Sub
    
    Private Sub text1_KeyDown(KeyCode As Integer, Shift As Integer)
        'for delete and backspace
        If KeyCode = vbKeyDelete Or KeyCode = 8 Then
        
        DelKey = True
        Exit Sub
    
        ElseIf KeyCode = vbKeyDown Then
            List1.SetFocus
        End If
    End Sub
    That is assuming, the listbox has already items on it. Try it out and see what fits your program best.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listbox Search!?

    A listbox and a combobox use almost the same exact code for this. Just use the different constant declarations. Different values but named similar CB_ or LB_

    Private Const LB_FINDSTRING As Long = &H18F
    Private Const LB_FINDSTRINGEXACT As Long = &H1A2

    Private Const CB_FINDSTRING As Long = &H14C
    Private Const CB_FINDSTRINGEXACT As Long = &H158
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Listbox Search!?

    How did a combo box make it into the question?

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Listbox Search!?

    Because I posted it.

    Just pointing out a useless fact.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width