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:

  1. Private Sub LoadNodes()  
  2.        'Set Treeview Image List for MBTreeViewExplorer  
  3.         SystemImageList.SetTreeViewImageList(MBTreeView, False)  
  4.         'New ShellItem to Load Desktop  
  5.         Dim m_shDesktop As ShellItem = New ShellItem()  
  6.         Dim tvwRoot As TreeNode = New TreeNode()  
  7.         tvwRoot.Name = m_shDesktop.Path  
  8.         tvwRoot.Text = m_shDesktop.DisplayName  
  9.         tvwRoot.ImageIndex = m_shDesktop.IconIndexNormal  
  10.         tvwRoot.SelectedImageIndex = m_shDesktop.IconIndexOpen  
  11.         tvwRoot.Tag = m_shDesktop  
  12.   
  13.         Dim arrChildren As ArrayList = m_shDesktop.GetAllDirectories  
  14.         For Each shChild As ShellItem In arrChildren  
  15.             Dim tvwChild As TreeNode = New TreeNode()  
  16.             tvwChild.Name = shChild.Path  
  17.             tvwChild.Text = shChild.DisplayName  
  18.             tvwChild.ImageIndex = shChild.IconIndexNormal  
  19.             tvwChild.SelectedImageIndex = shChild.IconIndexOpen  
  20.             tvwChild.Tag = shChild  
  21.             If shChild.IsFolder And shChild.HasSubFolders Then tvwChild.Nodes.Add("PH")  
  22.             tvwRoot.Nodes.Add(tvwChild)  
  23.         Next  
  24.         MBTreeView.Nodes.Clear()  
  25.         MBTreeView.Nodes.Add(tvwRoot)  
  26.         tvwRoot.Expand()   
  27. End Sub 
The following method handles Icons for the MBTreeViewExplorer:
  1. Public Shared Sub SetTreeViewImageList( _  
  2.         ByVal treeView As TreeView, _  
  3.         ByVal forStateImages As Boolean)  
  4.         Initializer()  
  5.         Dim wParam As Integer = LVSIL_NORMAL  
  6.         If forStateImages Then  
  7.             wParam = LVSIL_STATE  
  8.         End If  
  9.         Dim HR As Integer  
  10.         HR = SendMessage(treeView.Handle, _  
  11.                     TVM_SETIMAGELIST, _  
  12.                     wParam, _  
  13.                     m_smImgList)  
  14. 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

Next Recommended Readings