Click here to Skip to main content
15,885,782 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.7K   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: ?????? [modified] Pin
Amro M.Saad10-Apr-18 15:45
Amro M.Saad10-Apr-18 15:45 
QuestionExcelent article - Why using component? Pin
ajliaks21-Apr-08 19:37
ajliaks21-Apr-08 19:37 
Questionget the folder's fullpath [modified] Pin
chenjiahua1-Nov-07 4:19
chenjiahua1-Nov-07 4:19 
AnswerRe: get the folder's fullpath [modified] Pin
Joakim Anandh25-Nov-10 6:05
Joakim Anandh25-Nov-10 6:05 
QuestionRe: get the folder's fullpath Pin
Suplanus21-Mar-11 23:41
Suplanus21-Mar-11 23:41 
AnswerRe: get the folder's fullpath Pin
Joakim Anandh22-Mar-11 18:31
Joakim Anandh22-Mar-11 18:31 
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 
Since ExplorerTreeView is a UserControl, this won't work.

You can get access to Tree events by exposing the treeWnd object, which is derived from TreeView. I added a public property in ExplorerTreeView:

///
/// TreeView window in this user control.
///

public ExplorerTreeViewWnd TreeViewWnd
{
get { return treeWnd; }
}

Then, in addition, you need to make ExplorerTreeViewWnd a public class:

public class ExplorerTreeViewWnd : TreeView

After that, hook up the AfterSelect event in your Form:

explorerTreeView1.TreeViewWnd.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);



// Richard
=============================
Attilan Software Factory
www.attilan.com
Constructing software tools in C#, .NET, and Windows Forms.

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 
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 

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.