|
-
Nov 19th, 2007, 02:27 AM
#1
Thread Starter
Addicted Member
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.
-
Nov 19th, 2007, 06:51 AM
#2
Frenzied Member
Re: Listbox Search!?
Try doing a search for "Autocomplete"
I found one of my old threads that worked here
This will get you started.
-
Nov 19th, 2007, 07:24 AM
#3
Re: Listbox Search!?
 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:
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 Const LB_FINDSTRINGEXACT = &H1A2
Private Sub txtSearch_Change()
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
End Sub
Last edited by Hack; Nov 19th, 2007 at 08:00 AM.
-
Nov 19th, 2007, 07:42 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 19th, 2007, 08:00 AM
#5
Re: Listbox Search!?
Good point. 
Changed.
-
Nov 19th, 2007, 09:24 AM
#6
Frenzied Member
Re: Listbox Search!?
 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
-
Nov 19th, 2007, 09:34 AM
#7
Re: Listbox Search!?
Post 13?????
There are only 6, now 7, posts in this thread.
-
Nov 19th, 2007, 09:39 AM
#8
Frenzied Member
Re: Listbox Search!?
 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.
 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.
-
Nov 19th, 2007, 11:55 AM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 19th, 2007, 11:58 AM
#10
Re: Listbox Search!?
How did a combo box make it into the question?
-
Nov 19th, 2007, 12:01 PM
#11
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|