Click here to Skip to main content
Click here to Skip to main content

An "Explorer-Style" TreeView Control

, 21 Feb 2006
Rate this:
Please Sign up or sign in to vote.
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...

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:

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

Share

About the Author

MrPJ
Software Developer
United Kingdom United Kingdom
Software Engineering Undergraduate

Comments and Discussions

 
QuestionLicense PinmemberMember 896106529-May-12 0:01 
GeneralHuh!? - Nodes.Count - Allways return 1 PinmemberMr.Jinky29-Jan-11 9:44 
GeneralRe: Huh!? - Nodes.Count - Allways return 1 Pinmembercyntonix22-Mar-11 22:35 
QuestionHow to prevent that + being added to empty folders? PinmemberMr.Jinky27-Jan-11 23:26 
QuestionHow to get files in selected folder PinmemberDjibril8-Jul-10 0:42 
AnswerRe: How to get files in selected folder [modified] PinmemberJoakim Anandh25-Nov-10 6:54 
GeneralRe: How to get files in selected folder PinmemberStehtimSchilf23-Jan-11 13:50 
GeneralRe: How to get files in selected folder Pinmemberjunli27-Jan-11 17:07 
AnswerRe: How to get files in selected folder [modified] PinmemberJoakim Anandh27-Jan-11 23:59 
AnswerRe: How to get files in selected folder PinmemberMalcolm J Stewart18-May-11 4:48 
QuestionLicense? PinmemberMax Santos15-Jun-10 12:37 
GeneralAbout NetHood PinmemberAndrew Huang30-Apr-10 11:17 
AnswerRe: About NetHood [modified] PinmemberJoakim Anandh25-Nov-10 6:56 
QuestionFiltering from drive listing downwards only? Or filtering out ShellItems - Best Practice? Pinmembergrahamoneale30-Apr-10 4:05 
AnswerRe: Filtering from drive listing downwards only? Or filtering out ShellItems - Best Practice? PinmemberDanzence24-Jun-10 7:42 
QuestionExample of event override?? Please Help Pinmembersorenson10-Jun-09 4:41 
AnswerRe: Example of event override?? Please Help [modified] PinmemberJoakim Anandh25-Nov-10 7:02 
GeneralSomething wrong with loading the control in the form.Designer [modified] PinmemberLatencyXXX12-May-09 10:24 
GeneralRe: Something wrong with loading the control in the form.Designer PinmemberTheQueue17-Sep-10 9:23 
GeneralRe: Something wrong with loading the control in the form.Designer PinmemberFabrizio Stellato18-Jul-12 23:55 
SuggestionRe: Something wrong with loading the control in the form.Designer PinmemberMrGenie21-Feb-13 23:45 
GeneralThanks. Pinmembermike050527-Feb-09 10:41 
QuestionPlease help me [modified] Pinmemberamini_porroo7-Jan-09 10:26 
General?????? [modified] PinmemberSemyonPark13-Aug-08 22:22 
QuestionExcelent article - Why using component? Pinmemberajliaks21-Apr-08 20:37 
Questionget the folder's fullpath [modified] Pinmemberchenjiahua1-Nov-07 5:19 
AnswerRe: get the folder's fullpath [modified] PinmemberJoakim Anandh25-Nov-10 7:05 
QuestionRe: get the folder's fullpath Pinmembercyntonix22-Mar-11 0:41 
AnswerRe: get the folder's fullpath PinmemberJoakim Anandh22-Mar-11 19:31 
GeneralNo Events!!!! PinmemberMike_S_P_197931-Aug-06 20:04 
GeneralRe: No Events!!!! PinmemberRichard Guion16-Jan-07 13:47 
GeneralShellItem Pinmemberhousefreak_sls30-Aug-06 9:25 
GeneralExcellent Pinmemberafaz2-Aug-06 23:54 
GeneralDetecting items during the execution of GetSubFolders function PinmemberMehmet Caner11-Jul-06 8:03 
GeneralRe: Detecting items during the execution of GetSubFolders function PinmemberAndyLang13-Dec-06 18:38 
GeneralError message in Visual C# IDE Pinmemberjespdj13-Jun-06 11:33 
GeneralRe: Error message in Visual C# IDE Pinmemberjespdj13-Jun-06 12:08 
GeneralSmall Bug on ~ShellItem() PinmemberNicola Delfino20-Apr-06 6:56 
QuestionProblem with ILCombine on Win2K PinmemberAlex Terekhov17-Apr-06 9:35 
AnswerRe: Problem with ILCombine on Win2K Pinmemberjespdj15-Jun-06 10:51 
GeneralRe: Problem with ILCombine on Win2K Pinmembersur_uix7-Sep-06 4:09 
QuestionExecute item? PinmemberAndrewVos20-Mar-06 4:19 
AnswerRe: Execute item? PinmemberMrPJ21-Mar-06 8:57 
GeneralRe: Execute item? PinmemberAndrewVos21-Mar-06 15:28 
GeneralSet Path Pinmemberbillsfan1_200117-Mar-06 9:54 
QuestionRe: Set Path PinmembermauleTTT31-Mar-06 1:56 
GeneralRe: Set Path PinmemberMads Sandahl Skov7-Jun-07 0:49 
QuestionIcons do not update when user changes system appearance while program is running PinmemberthisIsReallyStupid14-Mar-06 5:02 
QuestionBug with Design tab ? PinmemberAkeltonn28-Feb-06 11:52 
AnswerRe: Bug with Design tab ? PinmemberMrPJ28-Feb-06 14:43 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 21 Feb 2006
Article Copyright 2006 by MrPJ
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid