TreeView Explorer Control For Windows Applications


Introduction

What is the MBTreeViewExplorer? This is a simple TreeView derived control that uses the Windows Explorer interface. It provides no added functionality aside from the standard TreeView control methods, properties and events, however it does provide a Shell Item class that can be used to extend this basic example for your own needs and it is also a good starting point for those wanting to get to grips with the system image list and Shell32 programming.

Background

MBTreeViewExplorer is an explorer that inherits all the properties of a simple TreeView control. The language used is VB.NET. Parts of the code are based on other CodeProject tutorials and code samples found elsewhere on the Internet. All of the code was written by me but some of the concepts are from other references and books.

What I think is best about this program is:

  1. It is a simple design that should help you to learn how to use the TreeView control.
  2. Compared with many of the other sample or demo programs available, it loads directories fast!
  3. The program only loads the necessary folders.
  4. Here are only two events and one method that drives the MBTreeViewExplorer control.

Code

The concept for this MBTreeViewExplorer came from the Windows Explorer. MBTreeViewExplorer consists of four classes, ShellAPI, Shell Item, ShellDLL and SystemImageList. I organized the methods of MBTreeViewExplorer into layers like the following.

The following method loads all the Folder Nodes for the MBTreeViewExplorer:

Private Sub LoadNodes()  
       'Set Treeview Image List for MBTreeViewExplorer  
        SystemImageList.SetTreeViewImageList(MBTreeView, False)  
        'New ShellItem to Load Desktop  
        Dim m_shDesktop As ShellItem = New ShellItem()  
        Dim tvwRoot As TreeNode = New TreeNode()  
        tvwRoot.Name = m_shDesktop.Path  
        tvwRoot.Text = m_shDesktop.DisplayName  
        tvwRoot.ImageIndex = m_shDesktop.IconIndexNormal  
        tvwRoot.SelectedImageIndex = m_shDesktop.IconIndexOpen  
        tvwRoot.Tag = m_shDesktop  
  
        Dim arrChildren As ArrayList = m_shDesktop.GetAllDirectories  
        For Each shChild As ShellItem In arrChildren  
            Dim tvwChild As TreeNode = New TreeNode()  
            tvwChild.Name = shChild.Path  
            tvwChild.Text = shChild.DisplayName  
            tvwChild.ImageIndex = shChild.IconIndexNormal  
            tvwChild.SelectedImageIndex = shChild.IconIndexOpen  
            tvwChild.Tag = shChild  
            If shChild.IsFolder And shChild.HasSubFolders Then tvwChild.Nodes.Add("PH")  
            tvwRoot.Nodes.Add(tvwChild)  
        Next  
        MBTreeView.Nodes.Clear()  
        MBTreeView.Nodes.Add(tvwRoot)  
        tvwRoot.Expand()   
End Sub  

The following method handles Icons for the MBTreeViewExplorer:

Public Shared Sub SetTreeViewImageList( _  
        ByVal treeView As TreeView, _  
        ByVal forStateImages As Boolean)  
        Initializer()  
        Dim wParam As Integer = LVSIL_NORMAL  
        If forStateImages Then  
            wParam = LVSIL_STATE  
        End If  
        Dim HR As Integer  
        HR = SendMessage(treeView.Handle, _  
                    TVM_SETIMAGELIST, _  
                    wParam, _  
                    m_smImgList)  
End Sub  

Using the Code


It is very easy to use the MBTreeViewExplorer in your application. Simply add the reference of the provided DLL to your application and just drag and drop.

History

  • MBTreeViewExplorer Version 1.0

Download

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s