Consulting

Results 1 to 5 of 5

Thread: Exclude Dir dot & dotdot from listbox

  1. #1
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location

    Exclude Dir dot & dotdot from listbox

    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

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    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
    Be as you wish to seem

  3. #3
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    Quote Originally Posted by Aflatoon View Post
    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

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    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
    Be as you wish to seem

  5. #5
    VBAX Regular
    Joined
    May 2015
    Location
    New Plymouth, New Zealand
    Posts
    38
    Location
    No worries! It works! Thanks so much Aflatoon, I did not realise you can use an IF statement without an END IF.

    Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •