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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Api to get the index of an item in a ComboBox

Status
Not open for further replies.

Fursten

Programmer
Joined
Dec 27, 2000
Messages
403
Location
PT
Hi,

Is there any APi that give us the index of comboBox item? For instance, I would say the item as a tring and then the APi would tell me is index in the combo?

PS: I need this to get the index of a itemdata.

Thank you

Sergio Oliveira
 
Are you asking how to get the index of a form control object? Or are you asking if a value is contained within a control on a form? I'm confused. Either way, this can be answered without the need for an API call. Neil Konitzer, President
Freisoft
 
Yes, there is an API call to do this. Here's an illustrative example:
[tt]
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Private Const CB_FINDSTRING = &H14C

Private Function GetCBIndexFromString(SearchCombo As ComboBox, strSearch As String) As Long
GetCBIndexFromString = SendMessage(SearchCombo.hwnd, CB_FINDSTRING, -1, strSearch)
End Function
[/tt]
 
OK. Here is the non-API code

Private Function SearchComboItemDataForValue(ByRef Combo As ComboBox, _
ByVal Value As Integer) As Integer
Dim i As Integer
Dim found As Boolean

found = False
For i = 0 To Combo.ListCount - 1
If Combo.ItemData(i) = Value Then
found = True
Exit For
End If
Next

If found Then
SearchComboItemDataForValue = i
Else
SearchComboItemDataForValue = -1
End If

End Function Neil Konitzer, President
Freisoft
 
I would go for the API code, since it's MUCH, MUCH and MUCH quicker than a for next loop...
 
The For...Next loop is very, very slow.
 
I agree that by nature, For...Next Loop procedures can be slow.

Here's another question though. What do you think the API function is doing to retrieve the index value? I'd be willing to wager that it is also doing some sort of loop procedure to iterate through the values.

Not trying to be a smart a$$, this really has me thinking. Neil Konitzer, President
Freisoft
 
Niel,

I would guarantee the API is running a for loop, and the code probably runs very similar to your example. The difference is the API is using a lower level language (C++), which is closer to machine language than VB is. VB is designed more the way a human thinks, so the compiler and the executable must translate more of the code before it is readable by the computer, thus the loop is slower.

Make sense? Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top