Add row to folder

How do I add a folder and then add a row with a listbox. I presume I am meant to use InsertRow but how do I know the index of the last inserted folder. Is there something like this in the docs other than the one that uses NthField.

I need something like:

listbox.addfolder "folder"
listbox.insertrow listbox.lastfolderindex, "item"

Thanks

I see this:
Hierarchical ListBoxesCreating a simple hierarchical ListBox is more involved than a two-column ListBox because you must manage hiding and displaying the sublist data. A simple way to do this is to assign the sublists to a “hidden” column in the ListBox and toggle the display of that data when the user double-clicks on a “parent” element.

You create a row with a disclosure triangle using the AddFolder method (rather than the AddRow method) and then set the Hierarchical property to True. See the Example for a simple hierarchical ListBox with one level.
From Xojo docs

Is there an API that makes what I am trying to achieve more convenient or am I underestimating the convenience of the technique above.

Thanks

You add rows to a folder in the ExpandRow event. If you need to do that explicitly, set the Expanded property to True and the ExpandRow event will fire:

http://documentation.xojo.com/index.php/ListBox.Expanded

Once you do as Greg suggests, you don’t need to worry about the index. Within the ExpandRow event, AddRow and AddFolder will add the next row to the folder that was expanded.

I will give it a try. THanks

Ah, got it working.

Thanks to Greg O’Lone and thankyou Kem.

Using this code in expand row:

  select case me.Cell(row, 0)
  case "Folder 1"
    InsertRow row, "Item 1", 1
  end select

This code in my InsertRow method with parameters; row as integer, text as string, indent as integer:

  blocks.InsertRow row + 1, text, indent
  blocks.RowTag(row) = indent

And this in CollapseRow:

  dim index as uint32 = row
  
  //comparing if indexed row has a higher indent than the collapsed row
  while me.RowTag(index) > me.RowTag(row)
    row = row + 1
    me.RemoveRow(index)
  wend

All seems good.

[quote=93069:@Greg O’Lone]You add rows to a folder in the ExpandRow event. If you need to do that explicitly, set the Expanded property to True and the ExpandRow event will fire:

http://documentation.xojo.com/index.php/ListBox.Expanded[/quote]

Well, that does work, but that’s not what I recommended. As I posted, use AddRow, not InsertRow, and they will be added as subrows to the main folder row, indents and all. And you won’t need the code in CollapseRow either as that will be taken care of for you.

Got you. I changed to be how you suggested. Thanks