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

Enumerate Combo Box. Get list of Combo Items 1

Status
Not open for further replies.

kennedymr2

Programmer
May 23, 2001
594
AU
I am trying to access a program running under windows., it
is written in vb, seems to be in vb6.

I need to...
==
1. Find the combo box handle.. This i can achieve ok.
*** thunderrt6 combobox handle=1216

Unable to...
2. Get a list of the items in the combo box, there may be up to 20 items in it.
3. I need to select say Tractors
Need to ensure that Tractors is in the combo listing drop down list.
Then click on the item (Tractor), if it is there.
-----------------------------------

How do i get a list of the items in the combo box???

Would appreciate some help with this function.

 
Hi,

The combo's .List property is an array containing all the values. The following code will return all the values stored:

For intItem = 0 to combo.ListCount - 1
msgbox "Item " & intItem & " = " & combo.List(intItem)
Next

There is a very valuable resource in the Help of VB6. Just show the form in the IDE, select the combo box in that from and press F1 and enjoy.
 
koala15
I am trying to get the contents of a combo box , of another program, via an api handle.
Is there a way of using the code to get at an api handle???
 

The code below illustrates firstly (DumpCombo) how to get ALL the entries in a combobox, and secondly (CBSelectExact)how to select a specific entry. They both work on tha assumption that you have got the hWnd of the combobox in question.



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_ERR = (-1)

Private Const CB_GETCOUNT = &H146
Private Const CB_GETLBTEXTLEN = &H149
Private Const CB_GETLBTEXT = &H148

Private Const CB_FINDSTRING = &H14C
Private Const CB_FINDSTRINGEXACT = &H158
Private Const CB_SETCURSEL = &H14E





Private Sub DumpCombo(ByVal hwndCombo As Long)
Dim smResult As Long
Dim wParam As Long
Dim lParam As String ' Note that we're cheating slightly

Dim ItemCount As Long
Dim lp As Long

smResult = SendMessage(hwndCombo, CB_GETCOUNT, 0, 0) ' How many items in combo?

If smResult <> CB_ERR And smResult > 0 Then 'No error and combo is not empty
ItemCount = smResult
For wParam = 0 To ItemCount - 1 ' For each item in combo
smResult = SendMessage(hwndCombo, CB_GETLBTEXTLEN, wParam, 0) ' find out how long the returned string will be
lParam = String(smResult, Chr$(0)) 'prepare string buffer
smResult = SendMessage(hwndCombo, CB_GETLBTEXT, wParam, ByVal lParam) ' get the string
' lParam now contains string from combo, so do what you want with it here
' In this example we merely dump to the imediate window
Debug.Print lParam
Next
End If
End Sub

Private Sub CBSelectExact(ByVal hwndCombo As Long, ByVal strSearch As String)
Dim smResult As Long

smResult = SendMessage(hwndCombo, CB_FINDSTRINGEXACT, 0, ByVal strSearch)
If smResult <> CB_ERR Then ' Item is there
smResult = SendMessage(hwndCombo, CB_SETCURSEL, smResult, 0)
End If

End Sub
 
Strongm...
Thankyou very much, the code is exactly what i am after.
Appreciate your help with this problem.

Regards kennedymr2
 
Sorry kennedymr2, I did not appreciate that you were trying to access a combo in another task.

Just a footnote: what you are trying should/will work in Windows9X but may not work in NT and 2000 due to the ring separation of NT/2000.

Another solution is to get the two tasks to talk to each other via DCOM/Com+, or via pipes or via memory resident files.

Have a look at Dan Appleman's book &quot;Visual Basic Programmer's Guide to the WIN32 API&quot;. Besides being an excellent API reference it has a whole section on communicating across task boundries.
 
It does work under NT4. Haven't actually tested my code on W2000, but it should also work perfectly well there.

We're just sending messages to windows; the ring fencing you are talking about doesn't apply as such.

Mike
 
I submit to your greater experience.

However have a look at Dan's book - his wisdom in these matters is excellent.

God's speed.
 
His wisdom isn't - not anymore.

Dan's orginal books were excellent. And they were certainly my intoduction to the API from VB (I still have his first book).

However later books were not rewrites with new insights, but merely a bunch of additional information tacked on to the original book(s) - a cheap way of making a quick profit. Not Dan's fault. Just the publishers.

As a result, more recent versions of the &quot;Visual Basic programmers Guide to the Win32 API&quot; (and I think the latest version is at least 2 years old) are unfortunately vaguely inaccurate (or at least mileading) and in some cases (scarily) 100% wrong.

Plus you need to understand where he is coming from. And that's a big point. UNDESTANDING how things work is very different from knowing stuff BY ROTE because a book said it was so.

Rgrds,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top