Click here to Skip to main content
15,887,328 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 327.8K   15.7K   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

 
GeneralRe: ASP.NET 2.0 Pin
MrPJ18-Feb-06 0:46
MrPJ18-Feb-06 0:46 
GeneralRe: ASP.NET 2.0 Pin
chrisv21-Feb-06 15:57
chrisv21-Feb-06 15:57 
GeneralExcellent! Pin
Mark Belles17-Feb-06 8:40
Mark Belles17-Feb-06 8:40 
GeneralRe: Excellent! Pin
MrPJ17-Feb-06 8:57
MrPJ17-Feb-06 8:57 
GeneralRe: Excellent! Pin
Mark Belles17-Feb-06 9:48
Mark Belles17-Feb-06 9:48 
GeneralRe: Excellent! Pin
MrPJ17-Feb-06 11:45
MrPJ17-Feb-06 11:45 
GeneralRe: Excellent! Pin
Paul Conrad25-Feb-06 10:39
professionalPaul Conrad25-Feb-06 10:39 
QuestionContext Menu Pin
Steve Hansen17-Feb-06 7:13
Steve Hansen17-Feb-06 7:13 
AnswerRe: Context Menu Pin
MrPJ17-Feb-06 8:18
MrPJ17-Feb-06 8:18 

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.