TreeView Demo
(IE Favorites 1)

in Visual Basic®



by Rick Meyer       Home

This very brief VB project demonstrates how to use a TreeView Control as simply as possible. For this demo the Internet Explorer Favorite Folders are displayed. The favorites are stored as directories in C:\Windows\Favorites. They are found and added with a recursive procedure using the FindFirstFile - FindNextFile API's. Succeeding demos also display your specific favorites in a ListView Control.
If you search the program you will find that these are all the TreeView commands:

TreeView1.Move 120, 120, 2300, 2900 'Position the control on the form
TreeView1.Nodes.Clear 'Clear the tree of all nodes
TreeView1.Nodes.Add , , curPath, newName 'Add the first node
TreeView1.Nodes.Add oldPath, tvwChild, curPath, newName 'Add subsequent nodes
TreeView1.Nodes.Item(1).Expanded = True 'Expand the first node for viewing

    Instructions for Building this Project:

1. Start a new standard exe.
2. On Form1 put a CommandButton named Command1.
3. On Form1 put a TreeView Control named TreeView.
4. Set the TreeView.LabelEdit property to Manual.

There is no need to position or size the above controls
on the form since that is all done in the Form_Load.

To find the TreeView Control click on the VB Menu->Project->Components and select Microsoft Windows Common Controls 6. After you click OK the TreeView Control (and some others) will appear in the toolbar usually on the left of the VB IDE (Integrated Development Environment).
Now you are ready for the code. Select all of the following code (by clicking on the word 'Option' three times) and copy it to the clipboard [Ctrl][Insert]. Then paste it into the code window of Form1 with [Shift][Insert]. Variable Key
% As Integer
& As Long
! As Single
# As Double
$ As String
Option Explicit

'===========================================================
' These are the API declarations needed for
'   the file searching operations
'===========================================================
Const FILE_ATTRIBUTE_DIRECTORY = &H10

Private Type FILETIME
  dwLowDateTime     As Long
  dwHighDateTime    As Long
End Type

Private Type WIN32_FIND_DATA
  dwFileAttributes  As Long
  ftCreationTime    As FILETIME
  ftLastAccessTime  As FILETIME
  ftLastWriteTime   As FILETIME
  nFileSizeHigh     As Long
  nFileSizeLow      As Long
  dwReserved0       As Long
  dwReserved1       As Long
  cFileName         As String * 260
  cAlternate        As String * 14
End Type

Private Declare Function FindFirstFile _
    Lib "kernel32" Alias "FindFirstFileA" ( _
    ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long
   
Private Declare Function FindNextFile _
    Lib "kernel32" Alias "FindNextFileA" ( _
    ByVal hFindFile As Long, _
    lpFindFileData As WIN32_FIND_DATA) As Long

Private Declare Function FindClose Lib "kernel32" _
    (ByVal hFindFile As Long) As Long

'===========================================================
' End Declarations - Start Procedures
'===========================================================
Private Sub GetFavorites(ByVal oldPath$, ByVal newName$, _
                            Optional start As Boolean)
    
    Dim fFirst&, fNext&, found$, curPath$
    Dim w As WIN32_FIND_DATA
    
    curPath = AddSlash(oldPath) & newName
    
    'The first thing is to add this item as a node.
    If start Then
        'Note this is the first node added so there is
        ' no need to identify the relative (first param)
        ' or relationship (second param)
        TreeView1.Nodes.Add , , curPath, newName
    Else
        'New node identity (key) is a string (3rd param).
        ' Here we are using curpath (which will be unique).
        ' The fourth param (newName) is what is displayed
        ' in the TreeView (not necessarily unique).
        TreeView1.Nodes.Add oldPath, tvwChild, _
                            curPath, newName
    End If
    
    'Now we search the node (which is a directory).
    '  If another directory is found then this Sub is
    '  called recursively to add that as a node.
    DoEvents
    fFirst = FindFirstFile(AddSlash(curPath) & "*", w)

    If fFirst Then
        Do
            found = TrimNull(w.cFileName)
            
            If w.dwFileAttributes And _
              FILE_ATTRIBUTE_DIRECTORY Then
                If found <> "." And found <> ".." Then
                    'This is the recursive call for
                    '  the sub-directory just found
                    GetFavorites curPath, found
                End If
            'Else
                'This is where the code will go for
                ' found files (url's) in later demos.
            End If
            
            DoEvents
            fNext = FindNextFile(fFirst, w)
        Loop While fNext
        
        fNext = FindClose(fFirst)
    End If
End Sub

Private Function AddSlash$(ByVal s$)
    If Len(s) Then
        If Right$(s, 1) <> "\" Then s = s & "\"
    End If
    
    AddSlash = s
End Function

Private Function TrimNull$(ByVal s$)
    Dim pos As Integer
    
    pos = InStr(s, Chr$(0))
    If pos Then s = Left$(s, pos - 1)
    
    TrimNull = s
End Function

Private Sub Command1_Click()
    Static working As Boolean
    'Do not allow click while loading
    If working Then Exit Sub
    working = True
    
    'Clear the tree of all nodes
    TreeView1.Nodes.Clear
        
    'We have to specify the path and node display
    ' text to get the ball rolling.
    GetFavorites "c:\windows\", "Favorites", True
    
    'Programatically show the first expansion.
    ' Note there is no 0 node.
    TreeView1.Nodes.Item(1).Expanded = True
    
    working = False
End Sub

Private Sub Form_Load()
    Move 1000, 1000, 4800, 3600
    TreeView1.Move 120, 120, 2300, 2900
    Command1.Move 3000, 1080, 1200, 375
    Command1.Caption = "Refresh"
End Sub