Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Listbox contents with only handle

Posted on 1998-08-28
8
Medium Priority
?
779 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Suggested Courses

670 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