PDA

View Full Version : [SOLVED] Exclude Dir dot & dotdot from listbox



lkelsen
07-15-2015, 05:32 PM
Hi team,

I have a TreeView which displays a windows folder directory, its sub-folders and files as nodes.
When I click on any node which represents a folder it will display the contents of that folder in a ListBox adjacent to the TreeView, it also displays the dot and dotdot which are the two representatives of the current folder and parent folder in windows. How do I exclude these two items from my ListBox?

In the past I have had the following code in the function:



For i = 1 To 3
Me.lstTV.RemoveItem (0)
Next i

This approach causes errors depending on certain events which occur in my interface and also to me seems messy.

This is the code that adds each directory item to the ListBox when a node is clicked on, using he NodeClick function:



Private Sub tvProjects_NodeClick(ByVal Node As MSComctlLib.Node)


Dim lstStrucTV As String, Arr As Variant, SelectedNode As MSComctlLib.Node, s As String, sPath As String, sPath2 As String, i As Integer

lstTV.Clear
Range("Q5").Clear


Set SelectedNode = frmProjTree.tvProjects.SelectedItem


With SelectedNode
s = .Text & vbCrLf
If Len(.Tag) > 0 Then
Arr = Split(.Tag, vbCrLf)
sPath = Arr(LBound(Arr) + 1) & "\"
sPath2 = Arr(LBound(Arr) + 1)
Else
End If


End With


If GetAttr(sPath2) And vbDirectory Then
'--sPath's are folders
lstStrucTV = dir(sPath & "*", vbDirectory)


'--populate the second level listbox with second level folder list
While lstStrucTV <> ""
lstTV.AddItem lstStrucTV
lstStrucTV = dir
Range("Q4").Clear
Range("Q3").Value = sPath
txtPath.Value = sPath
Wend
Else
Range("Q3").Clear
Range("Q4").Value = sPath2
txtPath.Value = sPath2
GoTo Quit
End If

If frmProjTree.tvProjects.SelectedItem.Children = 0 Then
GoTo Quit
End If

Quit:


End Sub

Aflatoon
07-16-2015, 03:08 AM
You need to test the values before adding them:

'--populate the second level listbox with second level folder list While lstStrucTV <> ""
If lstStrucTV <> "." And lstStrucTV <> ".." Then
lstTV.AddItem lstStrucTV
lstStrucTV = Dir
Range("Q4").Clear
Range("Q3").Value = sPath
txtPath.Value = sPath
End If
Wend

lkelsen
07-16-2015, 01:49 PM
You need to test the values before adding them:

'--populate the second level listbox with second level folder list While lstStrucTV <> ""
If lstStrucTV <> "." And lstStrucTV <> ".." Then
lstTV.AddItem lstStrucTV
lstStrucTV = Dir
Range("Q4").Clear
Range("Q3").Value = sPath
txtPath.Value = sPath
End If
Wend


Thanks Aflatoon, I thought that would work also but have already tried it with no luck. When I execute the userform it freezes and I have to ctrl + alt + del.

Cheers,

Luke

Aflatoon
07-16-2015, 01:54 PM
Apologies - that should read:

'--populate the second level listbox with second level folder list
While lstStrucTV <> ""If lstStrucTV <> "." And lstStrucTV <> ".." Then lstTV.AddItem lstStrucTV
lstStrucTV = Dir
Wend
Range("Q4").Clear
Range("Q3").Value = sPath
txtPath.Value = sPath

lkelsen
07-16-2015, 02:00 PM
No worries! It works! Thanks so much Aflatoon, I did not realise you can use an IF statement without an END IF.

Cheers!