Click here to Skip to main content
16,020,568 members
Articles / Programming Languages / C#
Article

An "Explorer-Style" TreeView Control

Rate me:
Please Sign up or sign in to vote.
4.62/5 (39 votes)
21 Feb 20062 min read 339.6K   15.8K   153   84
An article describing how to create an Explorer-style treeview with system icons.

ExplorerTreeView Demo

Introduction

This is a simple TreeView derived control that mimics the Windows Explorer interface. It provides no added functionality aside from the standard TreeView control methods, properties, and events, however it does provide a ShellItem 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

Originally, I started researching this topic as I wanted to develop an FTP client in C# that would implement a local system explorer similar to that of the Windows Explorer, but it soon became apparent that it was not going to be an easy undertaking. Thanks to CodeProject and other code repositories, I came across some good examples of how to achieve what I wanted, however the best examples were written in VB.NET or Delphi, so I decided to write a simple C# control based on what I'd found.

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 myself but some of the concepts were "borrowed".

Helpful Hints

For each node in the TreeView, an associated ShellItem object is created and stored in the node's "Tag" property. The ShellItem object allows you to retrieve information for the shell folder represented by the node, such as whether the folder has any sub-folders. For example, in the TreeView's "OnBeforeCollapse" event, we can obtain the ShellItem object to perform a simple test...

C#
ShellItem shNodeItem = (ShellItem)e.Node.Tag;
if (shNodeItem.HasSubFolder)
    // Do something...

We can also get the folder's IShellFolder interface from the ShellItem object using the ShellFolder property. Using this interface, we can do all sorts of nifty stuff, such as implementing shell context menus:

C#
// Get the ShellItem for this node.
ShellItem shNodeItem = (ShellItem)e.Node.Tag;

// Create an array of PIDL's for obtaining
// the IContextMenu interface pointer.
IntPtr[] arrPidls = new IntPtr[0]; 
arrPidls[0] = shNodeItem.PIDL;

// Request the interface pointer.
uint uRetVal = 
  shNodeItem.ShellFolder.GetUIObjectOf(IntPtr.Zero, 
  1, arrPidls, ref IID_IContextMenu, 
  IntPtr.Zero, out pCtx);
if (uRetVal != 0)
    Marshal.ThrowExceptionForHR((int)uRetVal);

...

Marshal.ReleaseComObject(pCtx);

History

None yet.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United Kingdom United Kingdom
Software Engineering Undergraduate

Comments and Discussions

 
SuggestionTreeView Control MFC Sample for Windows File Exploer Pin
Seven Stars6-Feb-17 18:44
Seven Stars6-Feb-17 18:44 
QuestionLicense Pin
Member 896106528-May-12 23:01
Member 896106528-May-12 23:01 
GeneralHuh!? - Nodes.Count - Allways return 1 Pin
Mr.Jinky29-Jan-11 8:44
Mr.Jinky29-Jan-11 8:44 
GeneralRe: Huh!? - Nodes.Count - Allways return 1 Pin
Suplanus22-Mar-11 21:35
Suplanus22-Mar-11 21:35 
QuestionHow to prevent that + being added to empty folders? Pin
Mr.Jinky27-Jan-11 22:26
Mr.Jinky27-Jan-11 22:26 
QuestionHow to get files in selected folder Pin
Djibril7-Jul-10 23:42
professionalDjibril7-Jul-10 23:42 
AnswerRe: How to get files in selected folder [modified] Pin
Joakim Anandh25-Nov-10 5:54
Joakim Anandh25-Nov-10 5:54 
GeneralRe: How to get files in selected folder Pin
StehtimSchilf23-Jan-11 12:50
StehtimSchilf23-Jan-11 12:50 
GeneralRe: How to get files in selected folder Pin
junli27-Jan-11 16:07
junli27-Jan-11 16:07 
AnswerRe: How to get files in selected folder [modified] Pin
Joakim Anandh27-Jan-11 22:59
Joakim Anandh27-Jan-11 22:59 
AnswerRe: How to get files in selected folder Pin
Malcolm J Stewart18-May-11 3:48
Malcolm J Stewart18-May-11 3:48 
QuestionLicense? Pin
Max Santos15-Jun-10 11:37
Max Santos15-Jun-10 11:37 
GeneralAbout NetHood Pin
Andrew Huang30-Apr-10 10:17
Andrew Huang30-Apr-10 10:17 
AnswerRe: About NetHood [modified] Pin
Joakim Anandh25-Nov-10 5:56
Joakim Anandh25-Nov-10 5:56 
QuestionFiltering from drive listing downwards only? Or filtering out ShellItems - Best Practice? Pin
grahamoneale30-Apr-10 3:05
grahamoneale30-Apr-10 3:05 
AnswerRe: Filtering from drive listing downwards only? Or filtering out ShellItems - Best Practice? Pin
Danzence24-Jun-10 6:42
Danzence24-Jun-10 6:42 
QuestionExample of event override?? Please Help Pin
sorenson10-Jun-09 3:41
sorenson10-Jun-09 3:41 
AnswerRe: Example of event override?? Please Help [modified] Pin
Joakim Anandh25-Nov-10 6:02
Joakim Anandh25-Nov-10 6:02 
GeneralSomething wrong with loading the control in the form.Designer [modified] Pin
LatencyXXX12-May-09 9:24
LatencyXXX12-May-09 9:24 
GeneralRe: Something wrong with loading the control in the form.Designer Pin
TheQueue17-Sep-10 8:23
TheQueue17-Sep-10 8:23 
GeneralRe: Something wrong with loading the control in the form.Designer Pin
Fabrizio Stellato18-Jul-12 22:55
Fabrizio Stellato18-Jul-12 22:55 
SuggestionRe: Something wrong with loading the control in the form.Designer Pin
MrGenie21-Feb-13 22:45
MrGenie21-Feb-13 22:45 
GeneralThanks. Pin
mike050527-Feb-09 9:41
mike050527-Feb-09 9:41 
QuestionPlease help me [modified] Pin
amini_porroo7-Jan-09 9:26
amini_porroo7-Jan-09 9:26 
General?????? [modified] Pin
SemyonPark13-Aug-08 21:22
SemyonPark13-Aug-08 21:22 

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.