-
Mar 12th, 2003, 04:13 PM
#1
VB - Search in combobox or listbox for a string
Add a listbox (List1) and a combobox (Combo1), a textbox (Text1) and two command buttons (Command1 , Command2) to your form and add the following code :
VB Code:
Option Explicit
Private Const CB_FINDSTRING = &H14C
Private Const LB_FINDSTRING = &H18F
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 Sub Command1_Click()
FindCB Combo1, Text1.Text
End Sub
Private Sub Command2_Click()
FindLB List1, Text1.Text
End Sub
Private Sub Form_Load()
Command1.Caption = "Find in Combo Box"
Command2.Caption = "Find in List Box"
Combo1.AddItem "one"
Combo1.AddItem "two"
Combo1.AddItem "three"
Combo1.AddItem "four"
Combo1.AddItem "five"
Combo1.AddItem "six"
Combo1.AddItem "seven"
List1.AddItem "one"
List1.AddItem "two"
List1.AddItem "three"
List1.AddItem "four"
List1.AddItem "five"
List1.AddItem "six"
List1.AddItem "seven"
End Sub
Private Sub FindLB(obj As Object, TextToFind As String)
obj.ListIndex = SendMessage( _
obj.hwnd, LB_FINDSTRING, -1, ByVal _
TextToFind)
End Sub
Private Sub FindCB(obj As Object, TextToFind As String)
obj.ListIndex = SendMessage( _
obj.hwnd, CB_FINDSTRING, -1, ByVal _
TextToFind)
End Sub
Last edited by manavo11; Mar 13th, 2003 at 07:32 AM.
Has someone helped you? Then you can Rate their helpful post.
-
Aug 17th, 2005, 05:57 PM
#2
Hyperactive Member
Re: VB - Search in combobox or listbox for a string
Hello manavo11,
Why do you have to use API's?
cant you just search it like this?
VB Code:
Public Function CheckIfExistInCombo(objCombo As Object,TextToFind As String) As Boolean
Dim NumOfItems As Variant 'The Number Of Items In ComboBox
Dim IndexNum As Integer 'Index
NumOfItems = objCombo.ListCount
For IndexNum = 0 To NumOfItems - 1
If objCombo.List(IndexNum) = TextToFind.Text Then
CheckIfExistInCombo = True
Exit Function
End If
Next IndexNum
CheckIfExistInCombo = False
End Function
is API faster than simple code?
I think its easier to debug the simple code and not the API function.
Best Regards,
ERAN
Eran Fox
ASSEMBLER,C,C++,VB6,SQL...
-
Aug 17th, 2005, 06:03 PM
#3
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
|