Click here to Skip to main content
15,867,453 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 326.6K   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

 
GeneralNo Events!!!! Pin
Mike_S_P_197931-Aug-06 19:04
Mike_S_P_197931-Aug-06 19:04 
GeneralRe: No Events!!!! Pin
Richard Guion16-Jan-07 12:47
Richard Guion16-Jan-07 12:47 
GeneralShellItem Pin
housefreak_sls30-Aug-06 8:25
housefreak_sls30-Aug-06 8:25 
GeneralExcellent Pin
Abolfazl Khusniddinov2-Aug-06 22:54
Abolfazl Khusniddinov2-Aug-06 22:54 
GeneralDetecting items during the execution of GetSubFolders function Pin
CLMan11-Jul-06 7:03
CLMan11-Jul-06 7:03 
GeneralRe: Detecting items during the execution of GetSubFolders function Pin
Andy Lang13-Dec-06 17:38
Andy Lang13-Dec-06 17:38 
GeneralError message in Visual C# IDE Pin
jespdj13-Jun-06 10:33
jespdj13-Jun-06 10:33 
GeneralRe: Error message in Visual C# IDE Pin
jespdj13-Jun-06 11:08
jespdj13-Jun-06 11:08 
Just found a similar thing in the constructor of ShellItem (line 26 of ShellItem.cs).
GeneralSmall Bug on ~ShellItem() Pin
CaledosLAB20-Apr-06 5:56
CaledosLAB20-Apr-06 5:56 
QuestionProblem with ILCombine on Win2K Pin
Alex Terekhov17-Apr-06 8:35
Alex Terekhov17-Apr-06 8:35 
AnswerRe: Problem with ILCombine on Win2K Pin
jespdj15-Jun-06 9:51
jespdj15-Jun-06 9:51 
GeneralRe: Problem with ILCombine on Win2K Pin
sur_uix7-Sep-06 3:09
sur_uix7-Sep-06 3:09 
QuestionExecute item? Pin
AndrewVos20-Mar-06 3:19
AndrewVos20-Mar-06 3:19 
AnswerRe: Execute item? Pin
MrPJ21-Mar-06 7:57
MrPJ21-Mar-06 7:57 
GeneralRe: Execute item? Pin
AndrewVos21-Mar-06 14:28
AndrewVos21-Mar-06 14:28 
GeneralSet Path Pin
billsfan1_200117-Mar-06 8:54
billsfan1_200117-Mar-06 8:54 
QuestionRe: Set Path Pin
mauleTTT31-Mar-06 0:56
mauleTTT31-Mar-06 0:56 
GeneralRe: Set Path Pin
Mads Sandahl Skov6-Jun-07 23:49
Mads Sandahl Skov6-Jun-07 23:49 
QuestionIcons do not update when user changes system appearance while program is running Pin
LewisG14-Mar-06 4:02
LewisG14-Mar-06 4:02 
QuestionBug with Design tab ? Pin
Akeltonn28-Feb-06 10:52
Akeltonn28-Feb-06 10:52 
AnswerRe: Bug with Design tab ? Pin
MrPJ28-Feb-06 13:43
MrPJ28-Feb-06 13:43 
GeneralDragDrop Pin
Ian MacLean22-Feb-06 9:02
Ian MacLean22-Feb-06 9:02 
GeneralRe: DragDrop Pin
MrPJ22-Feb-06 10:51
MrPJ22-Feb-06 10:51 
GeneralRe: DragDrop Pin
Ian MacLean22-Feb-06 11:02
Ian MacLean22-Feb-06 11:02 
GeneralRe: DragDrop Pin
MrPJ23-Feb-06 10:07
MrPJ23-Feb-06 10:07 

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.