Solved

Listbox contents with only handle

Posted on 1998-08-28
8
772 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 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