Hello, I am using Visual Studio 2008 to create a program that utilizes Window's API to interact with a window that pops up on an external program. I am trying to populate a ListBox with particular text using LB_ADDSTRING & LB_SETITEMDATA but I am not having any luck in doing so:

Dim hWnd As Integer = 0
        Dim itmData As Integer
        Dim numItems As Integer
        Dim sItemText As String = Space$(512)   'prepare the recieving buffer
        Dim retval As Integer = 0
        windowsCount = 0

        myCATIA.StartCommand("Customize...")
        'get parent window
        Do
            hWnd = FindWindow("#32770", "Customize")
        Loop Until hWnd > 0

        'look for child windows
        EnumChildWindow(hWnd, 0)     'one call directly to list parent
        EnumChildWindows(hWnd, AddressOf EnumChildWindow, 0)

        'temporarily prevent updating
        LockWindowUpdate(windowHandles(9))

        'get the number of items in the target list
        numItems = SendMessage(windowHandles(9), LB_GETCOUNT, 0&, Val(0&))
        'add the item text to the target list
        sItemText = "Assembly Design"
        SendMessage(windowHandles(9), LB_ADDSTRING, 0&, Val(sItemText))
        'add the item data to the target list
        SendMessage(windowHandles(9), LB_SETITEMDATA, numItems + 1, 0&)

        'allow redrawing
        LockWindowUpdate(0)

        PostMessage(hWnd, WM_CLOSE, 0&, 0&)

After I Enum all of the child windows, I can successfully get a count of the "RightList" ListBox using LB_GETCOUNT. But, my code won't let me add some text to it.Attached should be a screendump of the window I am interacting with. Any help would be appreciated.

Recommended Answers

All 7 Replies

The Val function returns the numeric value of a character string. therefore on line 25 the value of the 4th argument will be 0 not "Assembly Design"

I currently am using the format of the SENDMESSAGE to send an integer

Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
        (ByVal hWnd As Integer, _
        ByVal wMsg As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As Integer) As Integer

This is why I thought that I needed to convert the string to an integer value. I do have a version to send a string, but, it didn;t work as well:

Public Declare Function SendMessageStr Lib "user32.dll" Alias "SendMessageA" _
        (ByVal hWnd As Integer, _
        ByVal wMsg As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As String) As Integer

Do I have these declarations incorrect? If so, how should they be called out in Visual Studio 2008?

Yes, they are incorrect. All the parameters in your function declarations should be datatype "Long". All your local variables should probably be datatype "Long" just to be safe.
Line 25 should be

SendMessageStr(windowHandles(9), LB_ADDSTRING, 0&, sItemText)

Since the wParam parameter for the LB_SETITEMDATA is supposed to be the zero-based index for the list box, Line 27 should be:

SendMessage(windowHandles(9), LB_SETITEMDATA, numItems, 0&)

although I don't see the point in setting ItemData for your newly added item to zero. But that's your lookout, I guess.

Also, this assumes you have your LB_XXX constants defined with the correct values.

Hope this helps. Good luck!

Opps you pointed out one error... I wated to used LB_SETITEMDATA initially instead of **_GET********. Thanks for finding that.

But, after changing my datatypes from INTEGER TO LONG, I run into an Arithmetic operation resulted in an overflow. It seems that when I now try to Enum all of the child windows, one of the variables is coming in blank. Thus it is telling me to "Make sure that you are not dividing by zero."

I was under the impression that with this version of Visual Basic 2008 that the LONG datatype is no longer used...thus the reason for me changing to the INTEGER datatype. Should I be using something else?

Oop, sorry...I did my testing with VB6 (my bad).

However, the rest of the stuff I pointed out should work fine.

Does anyone know where I might be able to find some additional help with this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.