I am building an application that reads IMAP mail folders using curl and then puts them into a hierarchical tree structure using a TreeView control (Einhugur).
I can download the data fine and get a string array that looks a bit like this:
The dots represent the hierarchy. I’m struggling a bit with how to translate this data into entities that I can use with the addNode or insertNode methods for TreeView.
It feels like there should be an elegant (and probably existing) solution to taking structured data like this and adding it to a TreeeView, but I can’t find anything that might guide me. I have been thinking of making an array out of a line using string.split or nthfield, but I can’t get much further than that.
Does anyone here know of examples of how to do this? Any pointers will be much appreciated.
Create a window.
Add listbox1 (ordinary Xojo one will do… you may want to modify this for the Treeview control)
Set it to allow expandable rows
Add a property lines() as string to the window
In the Open event or somewhere, I populate the array of lines for demo purposes.
Window Open event:
//array lines() is a window variable
lines.append "INBOX.jisc.JISC mail"
lines.append "INBOX.jisc.JISC mail.keep"
//fill listbox with level 1 items:
var flds() as string
for x as integer = 0 to lines.lastindex
flds = lines(x).Split(".")
if flds.LastIndex = 0 then
listbox1.rowtagat(listbox1.LastAddedRowIndex) = flds(0)
In the listbox ExpandRow event:
//get info for this row, from the rowtag
var strParent as string = me.rowtagat(row)
var parflds() as string
parflds = strParent.split(".")
var flds() as string
var isChild as boolean
for x as integer = 0 to lines.LastIndex //read all the lines
flds = lines(x).Split(".") //chop them up
if flds.LastIndex = parflds.lastindex +1 then
//correct depth eg 3 flds needs 2 fld parent
isChild = true
for y as integer= 0 to parflds.lastindex
if parflds(y) <> flds(y) then isChild = false
if ischild then
me.AddExpandableRowAt(row+1,flds(flds.lastindex), flds.lastindex )
me.rowtagat(me.LastAddedRowIndex) = string.FromArray(flds,".")
Thank you Jeff, that works nicely. It also illuminates for me why I have a problem with the TreeView control; there is no obvious equivalent of the row tag that your code uses to keep track of the parent. Maybe I will try to make TreeView work in the same way that your code does, by populating the TreeView node at the time of opening it; I’d been trying to build the entire tree structure in one go.