Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you do a For each Item in ComboBox? 1

Status
Not open for further replies.

Glowworm27

Programmer
May 30, 2003
587
US
I want to iterate through the text in a comboBox.

is it possible to iterate through the combobox text with something like this

Dim tmpString as String
Code:
For Each tmpString in ComboBox1.list
  if tmpString = "Something" then 
     msgbox "found the item"
  End if
Next

or do I have to loop though the combobox using the ListCount ?

Thanks
[cannon]

George Oakes
Goakes@TiresPlus.com = Programmer
George@1-Specialday.com = Mobile DJ
Check out this awsome .Net Resource!
 
Not that I know of. Use the listcount.

Private Sub Command1_Click()
Dim FoundIt As Boolean
Dim i As Integer
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = "Something" Then
MsgBox "found the item"
FoundIt = True
Exit Sub
Else
FoundIt = False
End If
Next
If FoundIt = False Then MsgBox "didn't find the item"
End Sub

Swi
 
You will need to use the listcount like this:

Dim i As Integer
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = "Something" Then
MsgBox ("Found It!")
End If
Next

 
thats what I was afraid of.

Thats the way I am coding it now, just hoping for a faster way to check the items in the list.

I have over 30,000 item and it takes a minute to find it in the list.

Anyone know a faster way?

Just hoping I guess.

thanks SWI

George Oakes
Goakes@TiresPlus.com = Programmer
George@1-Specialday.com = Mobile DJ
Check out this awsome .Net Resource!
 
How did the 30,000 items get in there?
If from a datasource, you might be better doing a find there, or a select ...where...

If the combo contains SORTED data, you could implement a binary chop search (search the archives), which will knock your socks off when compared to the method you are using.
(Worse case scenario: 15 compares to find the thing you want.)
In general terms, a binary chop search looks at the value halfway down the list.
If it matches, stop.
If not, decide whether your item is higher or lower.
Half the number of records remaining, and check that one.
Repeat until you find the record, or you have less than 1 record to check.
 
The 30,000 items are an employee listing, from a datasource. The user is supposed to select the employee they want and then we will do something with that employee they selected.

i.e. add them to a work schedule so they can be assigned to work.

George Oakes
Goakes@TiresPlus.com = Programmer
George@1-Specialday.com = Mobile DJ
Check out this awsome .Net Resource!
 
I don't pay but try this:

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 CB_FINDSTRINGEXACT = &H158
Private Sub Command1_Click()
If FindValue(Combo1, "Test") = True Then
MsgBox "Item found!", vbInformation
Else
MsgBox "Item NOT found!", vbInformation
End If
End Sub

Function FindValue(Combo As ComboBox, StrToFind As String) As Boolean
Dim PosInCombo As Long
PosInCombo = SendMessage(Combo.hwnd, CB_FINDSTRINGEXACT, -1, ByVal CStr(StrToFind))
If PosInCombo = -1 Then
FindValue = False
Else
FindValue = True
End If
End Function

Swi
 
All I did was did a search on google and it popped that web page up. I just scrolled down the page and checked out some of the responses to the post. I think that maybe they are people's comments leading up to the solution because like you said you need to pay for the solution.

Swi
 
Thanks Swi,

gave you another Star for the find rountine, that was what I was looking for and was extremely fast for what I needed.

now if I could just finished this project that fast....


:)

thanks
[cannon]


George Oakes
Goakes@TiresPlus.com = Programmer
George@1-Specialday.com = Mobile DJ
Check out this awsome .Net Resource!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top