Interesting sorting problem with ListBox control 
Author Message
 Interesting sorting problem with ListBox control

Whenever you use the ListBox control with the Sort property set to True, it
will properly sort the information in the ListBox.  When it sorts, it will
change the index position of the added item as needed to maintain its
order.  The new index is never returned (to the best of my knowledge) to
the user in any manner after the sort is performed.

The result of this is that if you add a bunch of items to a ListBox that
automatically sorts, and you expect the index of those items to be reported
in the order that that they were added to the list, you will get unexpected
indices since the control rearranged things as it needed to keep things in
order.

Does anyone know how to find out how the indices were reordered or where it
inserted the new item programmatically?

[In my case, I have a form that searches your drive for a specific type of
file.  If it finds it, it opens the file and retrieves an ID string from it
(this is what goes into the list box), storing this ID string along with
the full path of the file into an array of types I have defined.  When a
user selects one of the listbox items, I want to use the selected index to
index off the array where the path and ID is stored, but if the list is
automatically alphabetized, the index is invalid.  All of the workarounds
I've found have some undesirable side effects to the useability of the
application (for instane: 1. could use the IDs to search though my array,
but unfortunately the ID's aren't unique, 2. could find all of the files
first, alphabetize the list, and then add them all to the listbox but
unfortunately I need to have updating to the list box as soon as the file
is found).  I know I could probably go and realphabetize the list as I add
them and then keep track of those indices, but, whew!, what a pain!]

Any comments would be appreciated.

Jason



Mon, 18 Sep 2000 03:00:00 GMT  
 Interesting sorting problem with ListBox control

With or without sorting .newindex property will tell you where the new item
went into the list.

The .Itemdata() array will hold longs and will be moved during a sort.

I put a listbox on a form and named it list1 and set .sorted = true then ran
the following code.

Private Sub Form_Load()
    With List1
        .AddItem "A"
            Debug.Print .NewIndex
        .AddItem "Z"
            Debug.Print .NewIndex
        .AddItem "D"
            Debug.Print .NewIndex
    End With
End Sub

debug window showed
0
1
1

Hope this explains it some....Write back if you want more help.



Mon, 18 Sep 2000 03:00:00 GMT  
 Interesting sorting problem with ListBox control

Quote:

> Whenever you use the ListBox control with the Sort property set to True, it
> will properly sort the information in the ListBox.  When it sorts, it will
> change the index position of the added item as needed to maintain its
> order.  The new index is never returned (to the best of my knowledge) to
> the user in any manner after the sort is performed.

> Does anyone know how to find out how the indices were reordered or where it
> inserted the new item programmatically?

> Any comments would be appreciated.

When you search the drive and find the file, you then have your file path and
string ID which you want to store in your UDT, with the ID being added to a
list box.

You code could look similar to the code below:
[Note: IDisValid() is a T/F function to text for valid ID (optional)
 The type is declared as MyType and contains FilePath and ID (plus others)
 MyUDT is the array of MyTypes]

If IDisValid(strID) then
        'make more space
        iNextItem = iNextItem + 1
        ReDim MyUDT(iNextItem) as MyType
        'Store values
        MyUDT.(iNextItem).FilePath = strPath
        MyUDT.(iNextItem).ID = strID
        'Display
        lstDisplay.AddItem strID
        lstDisplay.ItemData(lstDisplay.NewIndex) = iNextItem
End If

When the user clicks on a listbox item your code would then locate the
associated data using:

        Dim CurrentUDT as MyType

        CurrentUDT = MyUDT(lstDisplay.ItemData(lstDisplay.ListIndex))



Mon, 18 Sep 2000 03:00:00 GMT  
 Interesting sorting problem with ListBox control

Quote:

> [In my case, I have a form that searches your drive for a specific type of
> file.  If it finds it, it opens the file and retrieves an ID string from it
> (this is what goes into the list box), storing this ID string along with
> the full path of the file into an array of types I have defined.  When a
> user selects one of the listbox items, I want to use the selected index to
> index off the array where the path and ID is stored, but if the list is
> automatically alphabetized, the index is invalid.

When you do your .AddItem, put the index to your array
into .ItemData(.NewIndex)

Then when you want to look up the ID/Path strings,
use .ItemData(.ListIndex) instead of .ListIndex

        Bob O`Bob
--
"What do you want to reinstall today?"



Mon, 18 Sep 2000 03:00:00 GMT  
 Interesting sorting problem with ListBox control

Quote:

>Whenever you use the ListBox control with the Sort property set to True, it
>will properly sort the information in the ListBox.  When it sorts, it will
>change the index position of the added item as needed to maintain its
>order.  The new index is never returned (to the best of my knowledge) to
>the user in any manner after the sort is performed.

The NewIndex Property of the Listbox returns the value (index number) of
where it was inserted.It can be found in the help file.

Or am I on the wrong track ?
--
The Sounds of Red Dwarf http://www.swoon.demon.co.uk
Utilities for Turnpike http://www.swoon.demon.co.uk/other.htm
PGP KeyID: 0x2FB1D3D1



Tue, 19 Sep 2000 03:00:00 GMT  
 Interesting sorting problem with ListBox control

Since adding a new item to a sorted listbox reindex the list, your array will always be
wrong once the item has been added.  However, you do always know 2 things .. the listbox
count 'number' of the next available item (list1.ListCount+1), and the NewIndex of the
item.

I'd suggest that when an item is added, you use the NewIndex to add to the ItemData
property the actual array-index of that item.  On selection, ignore the ListIndex for
determining the array, and instead use that value stored in the ItemData property.

For example:

  thisitemsnumber = list1.ListCount+1
  List1.AddItem "Smith"
  List1.ItemData(List1.NewIndex) = thisitemsnumber
  yourtype(thisitemsnumber).spath = "c:\smith"

  thisitemsnumber = list1.ListCount+1
  List1.AddItem "Jones"
  List1.ItemData(List1.NewIndex) = thisitemsnumber
  yourtype(thisitemsnumber).spath = "c:\jones"

  thisitemsnumber = list1.ListCount+1
  List1.AddItem "White"
  List1.ItemData(List1.NewIndex) = thisitemsnumber
  yourtype(thisitemsnumber).spath = "c:\white"

On selecting the item, retrieve the ItemData to access the corresponding type for that
item ....

  selectedItem = List1.ListIndex
  actualArrayIndex = List1.ItemData(selecteditem)

  print yourtype(actualArrayIndex ).spath

--
Randy Birch, MVP Visual Basic
VBnet, The Visual Basic Developers Resource Centre
http://home.sprynet.com/sprynet/rasanen/vbnet/default.htm

Common Controls Replacement Project
http://www.mvps.org/ccrp

:
:Whenever you use the ListBox control with the Sort property set to True, it
:will properly sort the information in the ListBox.  When it sorts, it will
:change the index position of the added item as needed to maintain its
:order.  The new index is never returned (to the best of my knowledge) to
:the user in any manner after the sort is performed.
:
:The result of this is that if you add a bunch of items to a ListBox that
:automatically sorts, and you expect the index of those items to be reported
:in the order that that they were added to the list, you will get unexpected
:indices since the control rearranged things as it needed to keep things in
:order.
:
:Does anyone know how to find out how the indices were reordered or where it
:inserted the new item programmatically?
:
:[In my case, I have a form that searches your drive for a specific type of
:file.  If it finds it, it opens the file and retrieves an ID string from it
:(this is what goes into the list box), storing this ID string along with
:the full path of the file into an array of types I have defined.  When a
:user selects one of the listbox items, I want to use the selected index to
:index off the array where the path and ID is stored, but if the list is
:automatically alphabetized, the index is invalid.  All of the workarounds
:I've found have some undesirable side effects to the useability of the
:application (for instane: 1. could use the IDs to search though my array,
:but unfortunately the ID's aren't unique, 2. could find all of the files
:first, alphabetize the list, and then add them all to the listbox but
:unfortunately I need to have updating to the list box as soon as the file
:is found).  I know I could probably go and realphabetize the list as I add
:them and then keep track of those indices, but, whew!, what a pain!]
:
:Any comments would be appreciated.
:
:
:Jason



Tue, 19 Sep 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Sorting ListView Control (Interesting Problem)

2. Sorting ListView Contents (Interesting problem)

3. sorting listbox with sorted property on run time

4. Listbox/Combobox - .Sorted=True doesn't sort ItemData

5. ListBox and Sort problems

6. Problem with ListBox sort in Hebrew

7. Explorer ListBox control in sorted columns

8. Outlook 98: sorting entries in a ListBox control

9. How to sort integer numbers inside Listbox control

10. Interesting problem with DAO Data Control in Windows NT.

11. Interesting problem with control array and scrollable viewport

12. Fastest known sort for database access: any interest in VB implementation [updated]

 

 
Powered by phpBB® Forum Software