Solved

Listbox contents with only handle

Posted on 1998-08-28
8
777 Views
Last Modified: 2008-02-01
How can I get the contents of a listbox if all i have is the list box's window handle?
0
Comment
Question by:Bdsabian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Expert Comment

by:cip
ID: 1431744
You can use the SendMessage() API function to retrieve the content of a window listbox control.

The following code will retrieve the content of a listbox item only using the window handle of a visual basic listbox control. You should get the same result for any standard windows listbox.

- Draw a listbox control and a command button on your form.
- Paste this code:

Private Const LB_ERR = (-1)
Private Const LB_GETTEXT = &H189
Private Const LB_GETTEXTLEN = &H18A

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 Sub Command1_Click()
    Dim i As Integer
    Dim sItem As String, iLen As Integer
    Cont ITEMINDEX = 20
   
    ' Fill the listbox
    For i = 1 To 40
        List1.AddItem "Item " & i
    Next
   
    ' Get length of item 21 before
    ' retrieving it
    iLen = SendMessage(List1.hwnd, LB_GETTEXTLEN, ITEMINDEX, 0&)
    If iLen = LB_ERR Then
        MsgBox "Error retrieving item length"
        Exit Sub
    End If
   
    ' Allocate enough room in the
    ' string variable, then get item
    sItem = Space$(iLen)
    iLen = SendMessage(List1.hwnd, LB_GETTEXT, ITEMINDEX, ByVal sItem)
    If iLen = LB_ERR Then
        MsgBox "Error retrieving item"
        Exit Sub
    End If
   
    MsgBox "Item retrieved: " & sItem
   
End Sub

0
 

Author Comment

by:Bdsabian
ID: 1431745
that works for listboxes within my program, however, the listbox whose contents i need is in another program.  ive gotten that listbox's handle using a few API calls.
0
 
LVL 1

Expert Comment

by:cip
ID: 1431746
I thought you already had the listbox window handle, since this is what you state in your question, so I focused the answer about how to get the listbox contents.

However you can substitute list1.hwnd with the window handle you get with the API calls.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Bdsabian
ID: 1431747
I tried to substitute list1.hwnd with the handle i got using API calls, but it gave me garbage.  the first thing in the list box was Bdsabian, but what it returned to me was pŒ’
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1431748
Another thing to consider is to try to get the listbox handle from the owner program and compare it with the window handle you are getting from the 'client' application.  It may be that one window handle is a 16 bit number, and the other is a 32 bit handle.  This could especially be the case if one application is Win16 and the other is Win32,  or if a 'Win32' application actually uses a 'Win16' handle.  I've had a similar problem before.  I had a 32 bit application that worked fine under Win95, but didn't work under NT because a component in the application was returning a 16 bit handle.  NT was unforgiving, Win95 didn't care.

Just A Thought,
Martin
0
 
LVL 1

Accepted Solution

by:
cip earned 100 total points
ID: 1431749
Maybe you should provide more information. What API did you use? What application owns the listbox you're trying to access?

I didn't know a 32bit API call could return a 16bit handle as Martin said, I thought they would be converted to 32bit anyway, but this is worth checking.
0
 
LVL 1

Expert Comment

by:cip
ID: 1431750
Woops, I posted my last comment as an answer, please feel free to reject it.
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1431751
Actually it was an ActiveX component that was returning a device context to a printer.  The publisher admitted it was a bug and published an updated control.  It was weird that the buggy control worked under Win95 and not NT.  I just thought it may be a similar situation.

Later,
Martin

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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