Moving Expanded rows

I’ve been researching and messing around with expandable rows. Interesting stuff to be sure, but I’m starting to get the gist of how it works. I’ve noticed that you can’t use Xojo’s built in row reordering feature when you have a Listbox with expandable rows. Not a deal stopper for me, since I’m moving my rows programmatically with buttons. It’s kinda working the way I want with one big exception. Every time I move an expandable row (up in this example) it makes all the rows it passes through expandable rows as well. Now, these rows aren’t programmed to do anything with the disclosure widget, but still, they shouldn’t be marked as expandable rows. I’m not seeing in my code why this is happening, but obviously I’m missing something! Any ideas?

for x as integer = -1 to SCRIPT_LISTBOX.RowCount-1
  if SCRIPT_LISTBOX.RowSelectedAt(x) then
    if x < 1 then
      UpFinished=True
      return
    else
      dim LINEABOVE as string
      dim EXPANDROW as string
      LINEABOVE = SCRIPT_LISTBOX.CellTextAt(x-1) 
      EXPANDROW = SCRIPT_LISTBOX.CellTextAt(x)
      if EXPANDROW.BeginsWith (";Read") then
        
        SCRIPT_LISTBOX.RowExpandedAt(x) = false
        SCRIPT_LISTBOX.AddExpandableRowAt(x-1, EXPANDROW)
        SCRIPT_LISTBOX.RemoveRowAt(x)
        
      end
      SCRIPT_LISTBOX.CellTextAt(x-1) = SCRIPT_LISTBOX.CellTextAt(x)
      SCRIPT_LISTBOX.CellTextAt(x) = LINEABOVE   
      SCRIPT_LISTBOX.RowSelectedAt(x-1) = True
      SCRIPT_LISTBOX.RowSelectedAt(x) = False
      
    end if
  end
next

Better stil!

For XXX As Integer = SCRIPT_LISTBOX.RowCount-1 DownTo 0 
  MAIN.SCRIPT_LISTBOX.RowExpandedAt(XXX) = false
next

for x as integer = -1 to SCRIPT_LISTBOX.RowCount-1
  if SCRIPT_LISTBOX.RowSelectedAt(x) then
    if x < 1 then
      UpFinished=True
      return
    else
      dim LINEABOVE as string
      dim CURRENTROW as String
      LINEABOVE = SCRIPT_LISTBOX.CellTextAt(x-1) 
      CURRENTROW = SCRIPT_LISTBOX.CellTextAt(x)
      
      
      if SCRIPT_LISTBOX.RowExpandableAt(x)=true then
        
        SCRIPT_LISTBOX.RemoveRowAt(x)
        SCRIPT_LISTBOX.AddExpandableRowAt(x-1, CURRENTROW)
        SCRIPT_LISTBOX.RowSelectedAt(x-1) = True
        SCRIPT_LISTBOX.RowSelectedAt(x) = False
        
      else
        if SCRIPT_LISTBOX.RowExpandableAt(x-1)=true then
          SCRIPT_LISTBOX.RemoveRowAt(x-1)
          SCRIPT_LISTBOX.AddExpandableRowAt(x, LINEABOVE)
          SCRIPT_LISTBOX.RowSelectedAt(x-1) = True
          SCRIPT_LISTBOX.RowSelectedAt(x) = False
          
        else
          SCRIPT_LISTBOX.CellTextAt(x-1) = SCRIPT_LISTBOX.CellTextAt(x)
          SCRIPT_LISTBOX.CellTextAt(x) = LINEABOVE   
          SCRIPT_LISTBOX.RowSelectedAt(x-1) = True
          SCRIPT_LISTBOX.RowSelectedAt(x) = False
          
        end if
      end if
    end if
  end
next