Windows Handle.. List Combo Contents

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.


kennedymrAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Richie_SimonettiConnect With a Mentor IT OperationsCommented:
Since Khampton follow my comment, i should follow him:

Option Explicit

'notice that i put byval in front of lParam:
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_GETCOUNT = &H146
Private Const CB_GETLBTEXT = &H148

Private Sub Form_Load()
Dim i As Integer
For i = 0 To 10
    With Combo1
        .AddItem i
    End With
Next i
End Sub


Private Sub Command1_Click()
Dim MyList As Variant
MyList = GetListBoxItemArr(Combo1.hwnd)
Dim i As Long
For i = 0 To UBound(MyList)
   List2.AddItem MyList(i)
Next i
End Sub


Public Function GetListBoxItemArr(ByVal hwnd As Long) As Variant
Dim A() 'dynamic array
Dim s As String 'string of text from each item in the listbox
Dim i As Long, n As Long, ret As Long
'get the number of items:
Dim dummy As Long: dummy = 0
n = SendMessage(hwnd, CB_GETCOUNT, 0, dummy)
If n < 1 Then Exit Function
n = n - 1 'because we are working with zero based arrays, n now is the upperbound.
ReDim A(n) 'size the array to hold the contents of the listbox

For i = 0 To n
   s = String(255, 0) 'set this to the max. number of chars in an item (up to 32676).
   ret = SendMessage(hwnd, CB_GETLBTEXT, i, s)
   A(i) = s
Next i
GetListBoxItemArr = A
End Function
0
 
khamptonCommented:
Have you tried the API function:  getWindowText?
0
 
kennedymrAuthor Commented:
I have tried using getwindowtext, and i seem to only get the current value displayed in the combo box window.

I need to get a list of the underlying items in the dropdown of the combo box.

Maybe i am not completing the process completely.

regards  kennedymr
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Richie_SimonettiIT OperationsCommented:
You can't use getwindowstextapi, use sendmessage instead.
You need a sort of subclassing to get those values, i think.
More later.
0
 
khamptonCommented:
I have tested this code and it works.  Please verify and award the points to me.  Thanks.



Option Explicit
'HOW TO GET THE CONTENTS FROM A LISTBOX GIVEN ONLY ITS HWND.  BY KERRY HAMPTON.
'MAKE A FORM, ADD 2 LISTBOXES TO IT.

'notice that i put byval in front of lParam:
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 LB_GETTEXT = &H189
Private Const LB_GETCOUNT = &H18B

Private Sub Form_Load()
List1.AddItem "Dad"
List1.AddItem "Mom"
List1.AddItem "Sis"
List1.AddItem "Dog"
End Sub


Private Sub Command1_Click()
Dim MyList As Variant
MyList = GetListBoxItemArr(List1.hwnd)
Dim i As Long
For i = 0 To UBound(MyList)
    List2.AddItem MyList(i)
Next i
End Sub


Public Function GetListBoxItemArr(ByVal hwnd As Long) As Variant
Dim A() 'dynamic array
Dim s As String 'string of text from each item in the listbox
Dim i As Long, n As Long, ret As Long
'get the number of items:
Dim dummy As Long: dummy = 0
n = SendMessage(hwnd, LB_GETCOUNT, 0, dummy)
If n < 1 Then Exit Function
n = n - 1 'because we are working with zero based arrays, n now is the upperbound.
ReDim A(n) 'size the array to hold the contents of the listbox

For i = 0 To n
    s = String(255, 0) 'set this to the max. number of chars in an item (up to 32676).
    ret = SendMessage(hwnd, LB_GETTEXT, i, s)
    A(i) = s
Next i
GetListBoxItemArr = A
End Function

0
 
kennedymrAuthor Commented:
khampton

Appreciate all the work you have done, to get this info.
I have tried this with a list box, and all seems ok.

I am actually trying to get at a Combo Box, ..I do not seem to get any value in  n   , when i use a Combo Box.

Is the code any different to carry out the same function with a Combo Box.

Once again, appreciate you help

regards   kennedymr
0
 
kennedymrAuthor Commented:
I have now spent some time looking at the solution, it does work well, on the list box.The code is excellent.

I have tried to alter the code for a combo, but have had no success.

Would appreciate your advice as to how to apply to a combo.

Regards  kennedymr

0
 
TimCotteeCommented:
Richie: did you see this >>>> Please verify and award the points to me.  Thanks. <<<< From KHampton in earlier post, a bit pushy don't you think? (Especially from someone with 4 out of 11 questions asked still ungraded.)

Kerry, it is not considered good behaviour on this site, firstly to lock questions in the way that you have and secondly to press the questioner to award you the points. Please review the guidelines on comments vs answers and consider whether your posts have been following these guidelines or not.

Richie's solution seems to be to fit the requirements and I would suggest that you accept his answer to the question after rejecting the proposed answer by KHampton.
0
 
khamptonCommented:
TimCottee - what contribution are you making other that stirring up trouble?
0
 
khamptonCommented:
Sorry, I used LB_   type constants.  You can substitute CB_ constants instead... Just go to your API viewer, look up the equiv. CB_ constants and used them...

Good luck!
0
 
kennedymrAuthor Commented:
Sorry my question has got into a mix up!!!.

I appreciate both the answers.

Regards  kennedymr





0
 
kennedymrAuthor Commented:
Richie_Simonetti
Thankyou for the answer to my question.

I realise that the answer from  Khampton, was correct for listbox, and only needed a minor change to work on combobox.

--
As a non expert, i feel a little uncomfortable in this situation, as i really appreciate the help offered.


Kennedymr
0
 
khamptonCommented:
I feel that I supplied the code.  All that Richie did is copy my code and resubmitted it.  This is NOT fair!!!!
0
 
Richie_SimonettiIT OperationsCommented:
khampton. i would agree with you but let me clear some matters:

1) I agree with timcotte comment. It is not so nice ask for points.
2) it's true. I copied your code (it was a sort of punishment). What is also true, was that your earlier comment speaks on getwindowtext api and when i posted my comment regarding sendmessage, suddendly, you have changed your mind!
3) I would like to take one of the following two ways:
a) Reject my comment and accept kharmon answer.
b) Let me post a question Points for khampto) and give points to him.

You decide. I have no problem with any of them (a or b)

Cheers

PS: I apologize for my bad english.
0
 
khamptonCommented:
To: Richie Simonetti
From: khampton

* I agree that I worded the request for points was awkwardly phrased...  I ment to say that he should award points only if he verfied that my solution worked for him.

* My first response was just a hint - not a solution.  Your first response was also a hint!

* I'm truely sorry if I offended anyone by not following proper protocall but hey... this service is about helping each other NOT about fame!!!

* Thanks for being a gentleman about resolving my conflict...
0
 
Richie_SimonettiIT OperationsCommented:
Well, we don't need to go further with this...
kharmon, please, post the code with modifications and let kennedymr rejects my comment and accept yours.
By trhe way, since you have more time now, review your code an do a better design (I am NOT saying that it is bad! ;).
Cheers
0
 
khamptonCommented:
THIS IS THE FINAL VERSION OF MY CODE THAT WILL RETURN THE CONTENTS OF A COMBO BOX.....

Option Explicit
'HOW TO GET THE CONTENTS FROM A COMBOBOX GIVEN ONLY ITS HWND.  BY KERRY HAMPTON.
'MAKE A FORM, ADD A COMMAND BUTTON, A COMBO AND A LIST BOX...

'notice that i put byval in front of lParam:
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_GETCOUNT = &H146

Private Const CB_GETLBTEXT = &H148



Private Sub Form_Load()
Combo1.AddItem "Dad"
Combo1.AddItem "Mom"
Combo1.AddItem "Sis"
Combo1.AddItem "Dog"
End Sub


Private Sub Command1_Click()
Dim MyList As Variant
MyList = GetComboBoxItemArr(Combo1.hwnd)
Dim i As Long
For i = 0 To UBound(MyList)
   list1.AddItem MyList(i)
Next i
End Sub


Public Function GetComboBoxItemArr(ByVal hwnd As Long) As Variant
Dim A() 'dynamic array
Dim s As String 'string of text from each item in the listbox
Dim i As Long, n As Long, ret As Long
'get the number of items:
Dim dummy As Long: dummy = 0
n = SendMessage(hwnd, CB_GETCOUNT, 0, dummy)
If n < 1 Then Exit Function
n = n - 1 'because we are working with zero based arrays, n now is the upperbound.
ReDim A(n) 'size the array to hold the contents of the listbox

For i = 0 To n
   s = String(255, 0) 'set this to the max. number of chars in an item (up to 32676).
   ret = SendMessage(hwnd, CB_GETLBTEXT, i, s)
   A(i) = s
Next i
GetComboBoxItemArr = A
End Function


0
 
Richie_SimonettiIT OperationsCommented:
khampton, let me do a little thing and i would like your opinion:


Public Function GetComboBoxItemArr(ByVal hwnd As Long, ByRef lItemsFound as long) As string()
Dim arrItems() as string 'dynamic array
Dim s As String 'string of text from each item in the listbox
Dim i As Long, n As Long, ret As Long
'get the number of items:
Dim dummy As Long: dummy = 0
n = SendMessage(hwnd, CB_GETCOUNT, 0, dummy)
If n < 1 Then Exit Function
n = n - 1 'because we are working with zero based arrays, n now is the upperbound.
ReDim arrItems(n) 'size the array to hold the contents of the listbox

For i = 0 To n
  s = String(255, 0) 'set this to the max. number of chars in an item (up to 32676).
  ret = SendMessage(hwnd, CB_GETLBTEXT, i, s)
  arrItems(i) = trim$(s)
Next i

lItemsFound =i
GetComboBoxItemArr = arrItems
End Function

Then when you use this function....

Private Sub Command1_Click()
Dim MyList() As string, lcount as long
MyList = GetComboBoxItemArr(Combo1.hwnd, lcount)
Dim i As Long
For i = 0 To lcount
  list1.AddItem MyList(i)
Next i
End Sub

And kennedymr, please reject my comment. I wouldn't like to waste khampton's time.



0
 
khamptonCommented:
I like your improvement.  lcount would be equal to 0 if no items are present and the caller can check for this value rather than dealing with the error of getting the upperbound of the returned array.  (If the array is not redimensioned, it will be empty and thus an error? when you check the upperbound).

Cheers...
0
 
Richie_SimonettiIT OperationsCommented:
You are rigth!
Private Sub Command1_Click()
Dim MyList() As string, lcount as long
MyList = GetComboBoxItemArr(Combo1.hwnd, lcount)
if lcount=0 then
   msgbox "No items found!"
   exit sub
end if
Dim i As Long
For i = 0 To lcount
 list1.AddItem MyList(i)
Next i
End Sub

And i forgot something more:
......
lItemsFound =i -1 ' since we are 0 to n-1
GetComboBoxItemArr = arrItems
End Function



0
 
Richie_SimonettiIT OperationsCommented:
Khampton, since our partner didn't what we were talking, go to this link and answer the question:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20143476

0
All Courses

From novice to tech pro — start learning today.