Results 1 to 5 of 5

Thread: Sorted Listbox loses Itemdata

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    12

    Sorted Listbox loses Itemdata

    I'm using some listboxes which contain a string value as well as an itemdata which is storing the ID from a database.

    My problem is, when the listbox.sorted = true, the itemdata is lost when I try to get it based on the listindex. ie, lstBox.itemdata(lstBox.listindex))

    The sorted property of the listbox can only be set at design time. I can't find a way around this except to use a third party control such as VSFlexGrid.

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Use List1.ItemData(List1.NewIndex) instead when adding items to ur sorted list box.
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    12

    Didn't work

    BeachBum,

    Thanks for your help -- but I don't think that works. The itemdata are lost as soon as the .additem looping is finished (because the list is sorted). I tried what you suggested, but it didn't work because there are many items in the listbox which I then need the itemdata for when they are clicked on.

    Any other suggestions? I wish the .sorted property could be set at run time, that would fix it I think.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Try setting the .Sorted property to False, and sort the recordset the populates the listbox.

    In otherwords,

    SQL = "SELECT * FROM tablename ORDER BY whatever "
    Create recordset based on SQL statement.

    Send the sorted recordset through the List1.Additem loop

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Well am not sure how u are using then cos it does keep the correct itemdata when u use newindex.
    Try this and then adapt for ur needs
    Regards
    Stuart
    VB Code:
    1. Option Explicit
    2. '1 command button, 1 listbox and 1 label on form
    3. Private Sub Command1_Click()
    4.     With List1
    5.         .AddItem "z"
    6.         .ItemData(.NewIndex) = 1
    7.         .AddItem "a"
    8.         .ItemData(.NewIndex) = 2
    9.         .AddItem "g"
    10.         .ItemData(.NewIndex) = 3
    11.     End With
    12. End Sub
    13.  
    14. Private Sub List1_Click()
    15.     With List1
    16.         Label1.Caption = .List(.ListIndex) & " " & .ItemData(.ListIndex)
    17.     End With
    18. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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