CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    ListBox.ListCount

    While playing with this recursive directory search in the other thread I came upon something I never noticed in about ten years VB6 experience, and wanted to ask if someone knows it too, and can probably mention a remedy:

    Put a ListBox on a Form and in Form_Load() fil it with 32000 items.
    After that Debug.Print List1.ListCount. Looks good.
    Now fill it with 40000 items and you get printed -25536 which, as is obvious, is the representation of 40000 as a signed 16bit integer.

    More weird is the fact that the ListBox can easily accept more than 200000 items, but the .ListCount property always is within the signed 16 bit range.
    This seems to make the .ListCount property worthless as soon you expect more than 32767 entries in a ListBox.

    Anyone knows this phenomenon, or even better has a patch for it?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ListBox.ListCount

    Sure. It's a built-in limit. Can only address 32K items
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2001
    Posts
    6,332

    Re: ListBox.ListCount

    I get an error if I try to add more than 32767 items.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: ListBox.ListCount

    That's interesting. And funny. How do you add items, Wiz?
    I can add 500000 (half a million) items no problem and no error. And the items are really there, visible in the list.

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: ListBox.ListCount

    Quote Originally Posted by WoF View Post
    That's interesting. And funny. How do you add items, Wiz?
    I can add 500000 (half a million) items no problem and no error. And the items are really there, visible in the list.
    Using the AddItem method, of course.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: ListBox.ListCount

    I do:
    Code:
    Dim i&
    For i=1 To 500000: List1.AddItem Format(i, "000000"): Next
    without any problem or error.
    Only afterwards the .ListCount property behaves as I have described before.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: ListBox.ListCount

    That is odd. I have no idea how you could add that many items to the list without an error [bug in list box?] I wonder what would happen if you try to access the selected item when that item is deeper than 32k into the list?
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: ListBox.ListCount

    I just added 100k items to a listbox when I select the last item the text property returns the data selected. The List index property was around -31k
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: ListBox.ListCount

    Yes, i just tried too. The .Text property returns the selected item. ListIndex is, however, as scrambled as ListCount, like signed 16bit quantity.
    Accessing elements with an index above 32767 is impossible though.
    Print List1.List(50000) produces an overflow error.

    I think that's really weird.
    The ListBox accepts so many elements and does not allow to count or retrieve them.

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ListBox.ListCount

    Simple...

    Tells me David is 8642001

    Code:
    Option Explicit
    
    Private Sub Combo1_Click()
      Dim msg As String
    '  MsgBox Combo1.Text
      msg = Combo1.ItemData(Combo1.ListIndex) & " "
      msg = msg & Combo1.List(Combo1.ListIndex)
      MsgBox msg
    End Sub
    
    Private Sub Form_Load()
      ' Could also use ' List1.ItemData(0) = 5
      List1.AddItem "David G"
      List1.ItemData(List1.NewIndex) = 8642001 ' New Index
      List1.AddItem "Greg G"
      List1.ItemData(List1.NewIndex) = 8642000 ' New Index
      Combo1.AddItem "David G"
      Combo1.ItemData(Combo1.NewIndex) = 42001 ' New Index
      Combo1.AddItem "Greg G"
      Combo1.ItemData(Combo1.NewIndex) = 42000 ' New Index
    
    End Sub
    
    Private Sub List1_Click()
    ' Append Name to ItemData
      Dim msg As String
    ' msgbox list1.text
      msg = List1.ItemData(List1.ListIndex) & " "
      msg = msg & List1.List(List1.ListIndex)
      MsgBox msg
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: ListBox.ListCount

    Sorry, I don't quite get what it is you want to say with that code.

  12. #12
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: ListBox.ListCount

    Nor do I, Only 2 items in the list and the number is hard coded into item data, naturally it will return what you coded into it.
    Always use [code][/code] tags when posting code.

  13. #13
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ListBox.ListCount

    Well, if you add a million items, in order, then they'd return the index in order.

    EDIT: Then there's the Net Framework...
    Last edited by dglienna; May 20th, 2010 at 07:29 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: ListBox.ListCount

    return the index?

    the index is no longer valid once it exceeds the bounds allowed. Sure if you set the item data to the number which should be the index and someone clicks on that item you could get that number for all the good it would do you.

    You can't reference the listindex or list1.list(x) or listcount once it exceeds the bounds so I fail to see what the fact that you can add somthing larger into itemdata actually would be useful for in this case.
    Always use [code][/code] tags when posting code.

  15. #15
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ListBox.ListCount

    Just use an array() and adjust the lower and upper bounds to that of the listbox
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Page 1 of 2 12 LastLast

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