locked
vb.net listbox item count issue

    Question

  • I use a listBox on VB.NET (VS.NET 2005) and pass the listbox's handle to a C function for item population.

    Within the C function I use SendMessage() with LB_INSERTSTRING to insert item into the listbox, as in

    SendMessage((HWND)hWnd, LB_INSERTSTRING, lIndex, (LPARAM) ((LPCSTR) item_to_be_inserted));

    After the above call, I can verify the listCount is correct within the C function using LB_GETCOUNT, as in

    listCount = SendMessage((HWND)hWnd, LB_GETCOUNT, (WPARAM) 0, (LPARAM) ((LPCSTR)0));

    The problem is when execution returns to VB.NET, the listCount from VB.NET via listBox.Items.Count() always returnes a value of 0.

    This same code works on VB6, but does not work on VB.NET.

    Tuesday, February 07, 2006 8:56 PM

All replies

  • Perhaps you need your C function to return the data to put in the listbox ? It looks to me like the .NET listbox keeps an internal count instead of asking the underlying control.  I wonder why...

     

    Tuesday, February 07, 2006 8:58 PM
    Moderator
  • Hi,

    The items got populated all right, and shown from VB.NET application. However the items.count is always returned 0, so I cannot manipulate the listbox anymore.

    Any idea?

    Thank you much.

    David

     

     

     

    Tuesday, February 07, 2006 9:18 PM
  • I reiterate - the list box is obviously maintaining an internal count, which is pretty sloppy, but the way around it would seem to me to be to return the data you want to stuff into the listbox from your C dll ( I assume access to the data is the reason you do this at all ) and stuff it into the listbox within VB.NET.

     

    Tuesday, February 07, 2006 9:34 PM
    Moderator
  •  cgraus wrote:

    I reiterate - the list box is obviously maintaining an internal count, which is pretty sloppy, but the way around it would seem to me to be to return the data you want to stuff into the listbox from your C dll ( I assume access to the data is the reason you do this at all ) and stuff it into the listbox within VB.NET.

     

    That is definitely the problem (I have the same). I fill the listbox from a C++ dll, because i have to do a lot of preprocessig of the data before I send them to the listbox, which would be a mess in VB.

    Two simple functions inside the dll with SendMessage(hWnd, LB_GETCOUNT..) and SendMessage(hWnd, LB_GETTEXT) to retrieve the listbox contents later are a cheap workaround. However, I think that is a problem in VB and maybe should be solved.

    Monday, February 13, 2006 1:05 PM
  • Hi,

    We do a lot of processing under C as well. And we try not to change our current API if possible. Look like we will need to change the API anyway, to pass a array of BSTR back to VB.

    Thanks a lot for your help.

    David

    Tuesday, February 14, 2006 4:44 PM