This question is coming to my mind here and then since some people in this forum were talking about hierarchicalListbox.
I have a sample application that takes a folder (hard disk, MemoryStick, etc.) and display the first level contents and allow to open all Folders there (or so).
The question is What if I want to store the displayed data (from the Listbox) into disk.
More precisely, how to store the data in order to display them back at Open time
The only thing I have in mind is a two level store data; in a target folder (created at save time), create a file with all entries (standard entries added with AddRow) into the main text file, then create for each AddFolder entry a sub-folder and another text file who holds the ExpandRow data.
Of course, this may depends on how many level deep in the source folder one want to go.
And I do not know the target usage one can have too. (disk catalog list?)
Tree structures can be saved in a database. At a minimum you need a table with the following fields: recordID, displayText, level, parentID, plus perhaps a sequenceNumber. parentID is of course the recordID of the parent. When you expand a row you run a database query to get the records belonging to parentID. Store the recordID in the RowTag and level in a CellTag to facilitate the database query, which will look something like this (untested):
"SELECT * FROM mytable WHERE parentID = " + me.RowTag(row)+" AND level = " + me.CellTag(row,0)+" + 1 ORDER BY sequenceNumber"
If it is for display only, this is quite simple. It gets more complicated if you allow the user to modify the listbox (e.g. dragging rows around) and you want to save the changes back to the database, but it is still doable.
it’s raining day on Strasbourg today, but readine these answers was like having a break in the weather. ;-
Thank you all
Drop the Table, Create a new one and Save the contents ?
For non Expanded rows, one may think the user do not want to save them / one may want to save the contents anyay. Not tested / still at the idea time: what if I use the ExpandRow code to Expand them and save them, silently without any display ?
So, in the ExpandRow the method will be called with a Boolean parameter (say Display As Boolean = True) and the same method can be called with that parameter set to False for saving to file purposes.
Nice idea: now it may work (or not) / I may be able to code it (or not)
Define the data structure for your listbox first. For instance, I use a simple path “path/to/mailbox” . This is saved into a database. The expanded status is saved to the preferences as dictionary “path/to/mailbox” and a boolean for the expanded status. When loading data I load the database values first and then read the preferences.
Listbox has this neat function that returns a csv equivalent string using listbox.cell(-1, -1), I use this all the time to copy the listbox data to the clipboard where it can be pasted directly into Excel.
Tim is correct in that only expanded rows will have data, however there is now a rowisfolder property that can be used to expand all rows before saving, you just need to iterate through each row and If me.rowisfolder(x) then me.expanded(x) = true to expand all folder rows.
Reading that data back though is more problematical. In my case I do not, I regenerate the data from the source, and typically this is a database table with a foreign key relationship(s).
You might also consider that if what you are trying to save is the state that the user left the listbox in, that is, which folders are expanded, you could probably store just the sort order and the list of expanded items.