Funny Listbox behaviour

I have a funny listbox behaviour as follows.

I have a listbox, 4 columns, 23 rows. I want to sort the listbox contents. So I have a script that populates an array with the listbox contents, sorts the array and then repopulates the listbox.

Repopulating the listbox involves writing the new content into each listbox cell in turn.

What I have noticed is that if I add a new row to the listbox and add contents to the new cells, if the last entry keeps the cell open for editing, then the re-populate code incorrectly populates the last cell

Before sort
https://db.tt/Woda6iBP0Z

After sort
https://db.tt/J7SofcALJc

As you can see, the third cell in the last column, instead of having content tokyo.html, has the entered content for the god link.

I have tried setting the focus to another control but it does not appear to work. The only thing that works is clicking outside of the listbox but of course I can’t depend on the user doing that.

Anyone got any clues?

dim rownum, columnnum as integer
dim topicmapentries(-1), rowContent as string


for rownum = 1 to topicsListbox.ListCount - 1
  for columnnum = 0 to topicsListbox.ColumnCount
    if columnnum = 0 then
      rowContent = topicsListbox.Cell(rownum,columnnum)
    else
      rowContent = rowContent + "|" + topicsListbox.Cell(rownum,columnnum)
    end if
  next
  topicmapentries.Append(rowContent)
next

topicmapentries.Sort

dim rowCount as integer = topicmapentries.Ubound
dim columnCount as integer =  topicsListbox.ColumnCount - 1
for rownum = 0 to rowCount
  rowContent = ""
  for columnnum = 0 to columnCount
    rowContent = topicmapentries(rownum).NthField("|",columnnum+1)
    topicsListbox.Cell(rownum + 1,columnnum) = rowContent
  next
next

Some further info.

More testing indicates that if any cell is open for editing, then it’s contents are incorrectly overwritten. So the issue is not related to the last cell added, it occurs for any cell. Once the cell is closed for editing by clicking somewhere else in the listbox as long as you don’t click into a cell, the sort works OK

Just out of curiosity, why aren’t you using the built-in sorting capability? You can do custom sorting in the CompareRows event.

mmm, not aware of that. will have a look.

However, further testing shows that the problem is in the screen refresh because the debugger shows that the memory form of the listbox control is updated correctly. have tried a refresh in the control process but that does not appear to do anything.

bobj

Greg, my sort needs to be column 1 & column 2, so alphabetic by type and then names within type. The CompareRows event seems just as cumbersome as my way. Still, thanks for pointing that out to me, I hadn’t come across it before.

bobj

Have you tried

topicsListbox.SetFocus

or

ClearFocus

The problem is that the textfield used for the cell edit is only loosely tied to the listbox. If you change the listbox contents underneath it, it doesn’t know about it.

[quote=339339:@Bob Jansen]Greg, my sort needs to be column 1 & column 2, so alphabetic by type and then names within type. The CompareRows event seems just as cumbersome as my way. Still, thanks for pointing that out to me, I hadn’t come across it before.

bobj[/quote]
I doubt it. Pulling all of that data out, manipulating it and putting it back in sounds way worse to me. Just FYI, you can compare multiple columns and even convert the types in that event. It’s just code accessing the listbox.

This code sorts column 0 by alphabetical order. Column 1 is sorted for in alphabetical order for rows where the value in column 0 is the same. Much easier than what you are doing. Put this code in a button and try it out.

listbox1.ColumnSortDirection(1) = listbox.SortAscending
listbox1.SortedColumn = 1
listbox1.sort

listbox1.ColumnSortDirection(0) = listbox.SortAscending
listbox1.SortedColumn = 0
listbox1.sort

If the data in the Listbox comes from a DataBase, you can sort while loading the data and populate the Listbox.