Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / Visual Basic

TreeView Explorer using VB.NET 2008

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
7 Jul 2012CPOL1 min read 112.5K   15.9K   40   16
TreeView Explorer using VB.NET 2008
TreeViewExplorerVBNET2008/MBExplorerTest.JPG

Introduction

What is the MBTreeViewExplorer? This is a simple TreeView derived control which 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, which 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 which inherits all the properties of 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:

  1. It is a simple design, which 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 which drives the MBTreeViewExplorer control.

Code

TreeViewExplorerVBNET2008/MBExploreControl.gif

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

The following method loads all Folder Nodes for MBTreeViewExplorer:

VB.NET
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 MBTreeViewExplorer:

VB.NET
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

TreeViewExplorerVBNET2008/MBExploreToolBox.gif

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
It always seems good to hear about me, but the thing I do is I code. I'm Interested in Designing Windows Based Application, Web Applications and building Mobile Applications. Currently restricting it to Android 4.0 applications, building Internet Based Applications using ASP.NET and contributing to bring the student community to a position which will help technology to reach the greatest heights ever. A very Big fan of Microsoft & Android..!!

Comments and Discussions

 
QuestionHow can I just load special folder? Pin
Kai Lu17-Apr-15 0:08
Kai Lu17-Apr-15 0:08 
Questionget the folder tree of a ftp Pin
Member 1066825013-Mar-14 8:55
Member 1066825013-Mar-14 8:55 
GeneralMy vote of 1 Pin
bruun12-Feb-13 12:13
bruun12-Feb-13 12:13 
QuestionHow to create event myself. example after select Pin
newneo694-Dec-12 4:45
newneo694-Dec-12 4:45 
Questionhow getting files in the selected node? Pin
AlterDiaz22-Apr-12 0:49
AlterDiaz22-Apr-12 0:49 
AnswerRe: how getting files in the selected node? Pin
Member 92021302-Jul-12 23:03
Member 92021302-Jul-12 23:03 
GeneralRe: how getting files in the selected node? Pin
Manoj K Bhoir7-Jul-12 19:37
professionalManoj K Bhoir7-Jul-12 19:37 
GeneralRe: how getting files in the selected node? Pin
Member 906600810-Jul-12 4:36
Member 906600810-Jul-12 4:36 
GeneralRe: how getting files in the selected node? Pin
Manoj K Bhoir10-Jul-12 19:41
professionalManoj K Bhoir10-Jul-12 19:41 
GeneralMy vote of 5 Pin
Аslam Iqbal10-Mar-12 17:25
professionalАslam Iqbal10-Mar-12 17:25 
GeneralRe: My vote of 5 Pin
Manoj K Bhoir27-Mar-12 1:35
professionalManoj K Bhoir27-Mar-12 1:35 
Questionhow to retrive full path Pin
krruertyret4-Feb-12 2:36
krruertyret4-Feb-12 2:36 
AnswerRe: how to retrive full path Pin
Manoj K Bhoir10-Jul-12 19:44
professionalManoj K Bhoir10-Jul-12 19:44 
QuestionPirate Copy of Ra-One Pin
Irwan Hassan19-Dec-11 16:05
Irwan Hassan19-Dec-11 16:05 
AnswerRe: Pirate Copy of Ra-One Pin
Manoj K Bhoir30-Dec-11 20:07
professionalManoj K Bhoir30-Dec-11 20:07 
GeneralRe: Pirate Copy of Ra-One Pin
bruun12-Feb-13 12:03
bruun12-Feb-13 12:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.