CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 1999
    Posts
    3

    Negative Listcount

    I have a listbox with LOTS of items in it. It seems that when you have more than around 32768 entries in it, the Listcount starts going negative.. e.g. a listcount of 32769 would be returned as '-1'. Why the hell does it do this?! And how can I stop it?!

    Thanks,

    Nick



  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Negative Listcount

    unbelievable but true: Listcount is of type integer
    Use SsendMessage instead

    option Explicit
    private Const LB_GETCOUNT = &H18B
    private Declare Function SendMessageCallback Lib "user32" Alias "SendMessageCallbackA" (byval hwnd as Long, byval msg as Long, byval wParam as Long, byval lParam as Long, byval lpResultCallBack as Long, byval dwData as Long) as Long
    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 Long
    for i = 0 to 34000
    List1.AddItem "test"
    next i
    MsgBox List1.ListCount & "; " & i & ";" & SendMessage(List1.hwnd, LB_GETCOUNT, 0, 0)
    End Sub




    first value is negative, the other two are OK.



  3. #3
    Join Date
    Apr 1999
    Location
    Netherlands
    Posts
    181

    Re: Negative Listcount

    32768 is the maximum value of an integer. It seems that there are several ways that this is handles, sometimes you get a runtime error (overflow), or sometimes the value becomes negative... either -32767 or -1... It's because the listbox has a maximum of 32768 values (Index As Integer).
    I've seen some nice ideas how to solve this problem, one of them was i think the coolest, but I completely forgot where I've seen it... it used a class with an array to store the items, and they only fill the list with the items that are visible. Maybe someone else remembers seeing it....

    Crazy D :-)

  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Negative Listcount

    you could also use a ListView control instead of a list box. If you use it in Report Mode you get the same visual effect as in a listbox without the 32K limit.


  5. #5
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Negative Listcount

    Hi,

    Did you try this piece of your code?

    for i = 0 to 34000
    List1.AddItem "test"
    next i



    It never works. It would give overflow error for i = 32767.

    Even with API way of addition, LB_ADDITEM (?), there is some inherent limitation. Somewhere a 16-bit index is used and hence you cannoy have more than 32K of indecies. Look into MSDN.

    RK

  6. #6
    Join Date
    May 1999
    Posts
    3,332

    Re: Negative Listcount

    >Did you try this piece of your code?

    Yes, I did. Whenever I post code I have tested it before, unless otherwise specified.


  7. #7
    Join Date
    May 1999
    Posts
    3,332

    Re: Negative Listcount

    One more point:
    Did you see, that I Dimmed my "i" variable as long?
    Why would that cause an Overflow around 32000?
    Did you really try my code?

    Yes, ListIndex may also be restricted to the 32K limit.
    This is irrelevant if you use your Listbox only for displaying rows and not for trapping events.

    Also, I posted a suggested alternate solution: a ListView.


  8. #8
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Negative Listcount

    Ok. It Fails on my system - Win 95.

    Not because 'i' exceeds its limit ( it is dimed 'Long', i know), it fails at the line List1.Additem. WHen you break into debug mode on error, it highlights this line. ( atleast on my system! ).

    I think, because ListCount is also integer. So vb tries to update the Listcount property after adding the item, and probably it hits the block!

    Also if you look into MSDN, on list box section, occassionally they say the index should be 16bit only ( specific to Win95). So it is more 95-NT difference. I presume yours is a NT machine...

    I didn't comment about ListView. Did i ?:-) .
    Because listview.ListItems.Count is Long, it shouldn't pose problems.

    RK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured