How to get the number of ALL items of a hierarchical Listbox?

Hello,
How to get the number of ALL items of a hierarchical Listbox?
“Listbox.RowCount” returns me the number of visible items but not those that are “collapsed”.
Thanks.

I don’t think there is any simple way. I think you have to go to the data source for the “sub-rows” (whatever that might be) and figure it out from there.

As I understand heirarchical list boxes, they are essentially being built on demand. The user clicks on the reveal triangle and the developer has to figure out what rows to display when that happens. But that information is not actually “saved” in the structure of the listbox itself. It is built on the fly.

Don’t consider this to be definitive. I have used hierarchical listboxes but do not consider myself as expert. Certainly the way I use hierarchical listboxes does not allow Xojo’s listbox object to know “in advance” what all the sub-rows are. That information is supplied as only as a “need to know” situation. Unless the listbox has been ENTIRELY expanded and then you could use RowCount.

So, how can I declare, the number of elements to store???

Var myArray(-1) As Variant
ReDim myArray(MyListbox.RowCount) 

You don’t…

But if you explain what you are doing and why you want to know that we may better help.

I imagine a list of what’s in the Desktop folder,and ann eventual folders there are declared as expandable…

In this case if ypu want to know the number of item (including Desktop sub-folders), you have to count them by yourself an remove 1 for each Expandable Row.

But I would not code that this way; thus an explanation of what you want to do is welcome before been able to give an educated guess.

It is not about folders but about storing the elements of a hierarchical Listbox to send the array of elements to a function.

I still do not understand what you want to do.

A DesktopListBox is a displayer, not a storage bin. Use the data from where it is before displaying it in the DesktopListBox.

PS: I wrote "I imagine”; I’d better use “If for example…”.

I found this solution:

For Row As Integer =0 to Listbox1.RowCount-1
  if Listbox1.RowExpandableAt(row) AND Listbox1.RowExpandedAt(row) = False then
    Listbox1.RowExpandedAt(row)  = true
  end
Next

ReDim myArray(Listbox1.RowCount, Listbox1.RowCount)

Are-you sure it does not count the Expandable Rows in the returned values ?

Also, you will then read the DesktopListBox contents instead of the original data…

1/ very correct, I will check that…
2/ The original data is the one I stored in the DestopListbox!

Thanks you.

The data must have come from somewhere before you put it into the listbox, it is usually that you would count, rather than the listbox. Using a visual element, such as a listbox, is usually a very bad way of counting your data. All sorts of paint event and additional code would run in order to populate the listbox, resulting in very slow code.

As for the listbox the non-expanded rows don’t exist when they are not expanded. They are not there and simply ‘closed’, they are removed completely. You have to put them back when they are ‘opened’ again. In order to do that you need something to keep track of what you are going to put back.

1 Like

Exactly. All the data for my mailbox hierarchy is in an sqlite database, which gets consulted whenever the user clicks the discolsure triangle, in order to create the newly-visible rows. If one of the disclosed items has a discolosure triangle which is open, then I have to create those rows too.

‘Collapsed’ rows do not contain anything.
When they are expanded, it is your job to go find out what they should contain, and add the exposed rows in code.

Imagine the listbox represents a street.
You start with 5 collapsed rows which represent houses
You do not get a count of ‘houses plus the people who live in them’, unless you go look at the actual houses themselves.
When someone expands the ‘first house’, you find the names of the 3 people who live there and add them.
Now you have 5 houses and 3 people, but no idea how many people are in the next house, or the rest of the street

The data that is used to fill the listbox is where you do the count.
Counting the visible rows is only useful if you want to iterate over the visible listbox.

Worst:
imagine a ListBox with 100,000 Rows (if all are loaded and created) with, say 10 Columns with tons oc characters…
That would be a huge job to display all of that.
So, you decide to create code to display “data on demand”: only 50 Rows are displayed during the application run (or that window existence).
How can you count the number of Rows from the ListBox ?
(I can tell you the value: 50 !)

1 Like