# Hierarchical Lisbox - expand all folders

Hi there… I have an hierarchical listbox where I parse an xml file. So I need to have all folders expanded but my code seems work bad and (after hours) I don’t understand why… Thanks in advance for your help…

Here my code:

[code]dim i,o,u As Integer

for i=1 to 5 'because I have maximum 5 inner folders but I can’t know how many folders I’ll have
u=mylist.LastIndex
for o=0 to u
if mylist.RowIsFolder(o) then
mylist.Expanded(o)=true
end if
Next
Next[/code]

what purpose is you “I” loop… nowhere do you ever use its value

Simply do a loop and expand all entries?

[code]
For i As Integer = 0 To Listbox1.ListCount - 1

Listbox1.Expanded(i) = True

Next[/code]

It was for repeat the loop for 5 times because I have 5 inner levels of subfolder

[quote=415370:@Stefan Adelsberger]Simply do a loop and expand all entries?

[code]
For i As Integer = 0 To Listbox1.ListCount - 1

Listbox1.Expanded(i) = True

Next[/code][/quote]
Ok using listcount instead of lastindex seems working nice, but I have to execute the loop many times to expand all subfolders. I think I’ll use a “loop until” …
Nice suggestion. Thanks

You need to do the expansion recursively.

Here the definitive solution… Thanks to Norman Palardy! @Norman Palardy

[code]Dim toExpand() As Integer

For i As Integer = mylist.ListCount-1 DownTo 0
If mylist.RowIsFolder(i) And mylist.Expanded(i) = False Then
toExpand.Append i
End If
Next

While toExpand.Ubound >= 0
// expand all these
For i As Integer = toexpand.Ubound DownTo 0
mylist.Expanded( toExpand(i) ) = True
Next

Redim toExpand(-1)

// get whats left to expand
For i As Integer = mylist.ListCount-1 DownTo 0
If mylist.RowIsFolder(i) And mylist.Expanded(i) = False Then
toExpand.Append i
End If
Next

Wend[/code]

[quote=415491:@Sergio Tamborini]Here the definitive solution… Thanks to Norman Palardy! @Norman Palardy

[code]Dim toExpand() As Integer

For i As Integer = mylist.ListCount-1 DownTo 0
If mylist.RowIsFolder(i) And mylist.Expanded(i) = False Then
toExpand.Append i
End If
Next

While toExpand.Ubound >= 0
// expand all these
For i As Integer = toexpand.Ubound DownTo 0
mylist.Expanded( toExpand(i) ) = True
Next

Redim toExpand(-1)

// get whats left to expand
For i As Integer = mylist.ListCount-1 DownTo 0
If mylist.RowIsFolder(i) And mylist.Expanded(i) = False Then
toExpand.Append i
End If
Next

Wend[/code][/quote]

Did you try this simple loop?
I wrote an example project and it works:

[code]
For i As Integer = 0 To Listbox1.ListCount - 1

Listbox1.Expanded(i) = True

Next[/code]

[quote=415679:@Stefan Adelsberger]Did you try this simple loop?
I wrote an example project and it works:

[code]
For i As Integer = 0 To Listbox1.ListCount - 1

Listbox1.Expanded(i) = True

Next[/code][/quote]

Did you try with nested folders?

Yes I tried (see sample project).

[quote=415679:@Stefan Adelsberger]Did you try this simple loop?
I wrote an example project and it works: