Click to See Complete Forum and Search --> : Negative Listcount


Danny Boy
August 9th, 1999, 08:04 AM
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

Lothar Haensler
August 9th, 1999, 08:14 AM
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.

Crazy D @ Work
August 9th, 1999, 08:18 AM
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 :-)

Lothar Haensler
August 9th, 1999, 08:32 AM
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.

Ravi Kiran
August 10th, 1999, 06:35 AM
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

Lothar Haensler
August 10th, 1999, 06:56 AM
>Did you try this piece of your code?

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

Lothar Haensler
August 10th, 1999, 08:11 AM
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.

Ravi Kiran
August 11th, 1999, 05:18 AM
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