Click here to Skip to main content
Click here to Skip to main content
Go to top

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 896106528-May-12 23:01 
GeneralHuh!? - Nodes.Count - Allways return 1 PinmemberMr.Jinky29-Jan-11 8:44 
GeneralRe: Huh!? - Nodes.Count - Allways return 1 Pinmembercyntonix22-Mar-11 21:35 
QuestionHow to prevent that + being added to empty folders? PinmemberMr.Jinky27-Jan-11 22:26 
QuestionHow to get files in selected folder PinmemberDjibril7-Jul-10 23:42 
AnswerRe: How to get files in selected folder [modified] PinmemberJoakim Anandh25-Nov-10 5:54 
GeneralRe: How to get files in selected folder PinmemberStehtimSchilf23-Jan-11 12:50 
GeneralRe: How to get files in selected folder Pinmemberjunli27-Jan-11 16:07 
AnswerRe: How to get files in selected folder [modified] PinmemberJoakim Anandh27-Jan-11 22:59 
AnswerRe: How to get files in selected folder PinmemberMalcolm J Stewart18-May-11 3:48 
QuestionLicense? PinmemberMax Santos15-Jun-10 11:37 
GeneralAbout NetHood PinmemberAndrew Huang30-Apr-10 10:17 
AnswerRe: About NetHood [modified] PinmemberJoakim Anandh25-Nov-10 5:56 
QuestionFiltering from drive listing downwards only? Or filtering out ShellItems - Best Practice? Pinmembergrahamoneale30-Apr-10 3:05 
AnswerRe: Filtering from drive listing downwards only? Or filtering out ShellItems - Best Practice? PinmemberDanzence24-Jun-10 6:42 
QuestionExample of event override?? Please Help Pinmembersorenson10-Jun-09 3:41 
AnswerRe: Example of event override?? Please Help [modified] PinmemberJoakim Anandh25-Nov-10 6:02 
GeneralSomething wrong with loading the control in the form.Designer [modified] PinmemberLatencyXXX12-May-09 9:24 
GeneralRe: Something wrong with loading the control in the form.Designer PinmemberTheQueue17-Sep-10 8:23 
GeneralRe: Something wrong with loading the control in the form.Designer PinmemberFabrizio Stellato18-Jul-12 22:55 
SuggestionRe: Something wrong with loading the control in the form.Designer PinmemberMrGenie21-Feb-13 22:45 
GeneralThanks. Pinmembermike050527-Feb-09 9:41 
QuestionPlease help me [modified] Pinmemberamini_porroo7-Jan-09 9:26 
General?????? [modified] PinmemberSemyonPark13-Aug-08 21:22 
QuestionExcelent article - Why using component? Pinmemberajliaks21-Apr-08 19:37 

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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 21 Feb 2006
Article Copyright 2006 by MrPJ
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid