Solved

Windows Handle.. List Combo Contents

Posted on 2001-06-22
21
985 Views
Last Modified: 2007-11-27
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.


0
Comment
Question by:kennedymr
  • 8
  • 7
  • 5
  • +1
21 Comments
 
LVL 1

Expert Comment

by:khampton
ID: 6221027
Have you tried the API function:  getWindowText?
0
 

Author Comment

by:kennedymr
ID: 6221046
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6222249
You can't use getwindowstextapi, use sendmessage instead.
You need a sort of subclassing to get those values, i think.
More later.
0
 
LVL 1

Expert Comment

by:khampton
ID: 6222390
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
 

Author Comment

by:kennedymr
ID: 6222447
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
 

Author Comment

by:kennedymr
ID: 6222520
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6223299
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 6224176
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
 
LVL 1

Expert Comment

by:khampton
ID: 6225189
TimCottee - what contribution are you making other that stirring up trouble?
0
 
LVL 1

Expert Comment

by:khampton
ID: 6225223
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:kennedymr
ID: 6225273
Sorry my question has got into a mix up!!!.

I appreciate both the answers.

Regards  kennedymr





0
 

Author Comment

by:kennedymr
ID: 6225304
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
 
LVL 1

Expert Comment

by:khampton
ID: 6225487
I feel that I supplied the code.  All that Richie did is copy my code and resubmitted it.  This is NOT fair!!!!
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6225795
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
 
LVL 1

Expert Comment

by:khampton
ID: 6227746
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6227904
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
 
LVL 1

Expert Comment

by:khampton
ID: 6228611
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6229080
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
 
LVL 1

Expert Comment

by:khampton
ID: 6229301
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6229397
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6238693
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now