Click here to Skip to main content
11,483,586 members (69,106 online)
Click here to Skip to main content

An "Explorer-Style" TreeView Control

, 21 Feb 2006 216.9K 9.9K 144
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 Pin
Member 896106529-May-12 0:01
memberMember 896106529-May-12 0:01 
GeneralHuh!? - Nodes.Count - Allways return 1 Pin
Mr.Jinky29-Jan-11 9:44
memberMr.Jinky29-Jan-11 9:44 
GeneralRe: Huh!? - Nodes.Count - Allways return 1 Pin
cyntonix22-Mar-11 22:35
membercyntonix22-Mar-11 22:35 
QuestionHow to prevent that + being added to empty folders? Pin
Mr.Jinky27-Jan-11 23:26
memberMr.Jinky27-Jan-11 23:26 
QuestionHow to get files in selected folder Pin
Djibril8-Jul-10 0:42
memberDjibril8-Jul-10 0:42 
AnswerRe: How to get files in selected folder [modified] Pin
Joakim Anandh25-Nov-10 6:54
memberJoakim Anandh25-Nov-10 6:54 
GeneralRe: How to get files in selected folder Pin
StehtimSchilf23-Jan-11 13:50
memberStehtimSchilf23-Jan-11 13:50 
GeneralRe: How to get files in selected folder Pin
junli27-Jan-11 17:07
memberjunli27-Jan-11 17:07 
AnswerRe: How to get files in selected folder [modified] Pin
Joakim Anandh27-Jan-11 23:59
memberJoakim Anandh27-Jan-11 23:59 
AnswerRe: How to get files in selected folder Pin
Malcolm J Stewart18-May-11 4:48
memberMalcolm J Stewart18-May-11 4:48 
QuestionLicense? Pin
Max Santos15-Jun-10 12:37
memberMax Santos15-Jun-10 12:37 
GeneralAbout NetHood Pin
Andrew Huang30-Apr-10 11:17
memberAndrew Huang30-Apr-10 11:17 
AnswerRe: About NetHood [modified] Pin
Joakim Anandh25-Nov-10 6:56
memberJoakim Anandh25-Nov-10 6:56 
QuestionFiltering from drive listing downwards only? Or filtering out ShellItems - Best Practice? Pin
grahamoneale30-Apr-10 4:05
membergrahamoneale30-Apr-10 4:05 
AnswerRe: Filtering from drive listing downwards only? Or filtering out ShellItems - Best Practice? Pin
Danzence24-Jun-10 7:42
memberDanzence24-Jun-10 7:42 
QuestionExample of event override?? Please Help Pin
sorenson10-Jun-09 4:41
membersorenson10-Jun-09 4:41 
AnswerRe: Example of event override?? Please Help [modified] Pin
Joakim Anandh25-Nov-10 7:02
memberJoakim Anandh25-Nov-10 7:02 
GeneralSomething wrong with loading the control in the form.Designer [modified] Pin
LatencyXXX12-May-09 10:24
memberLatencyXXX12-May-09 10:24 
GeneralRe: Something wrong with loading the control in the form.Designer Pin
TheQueue17-Sep-10 9:23
memberTheQueue17-Sep-10 9:23 
GeneralRe: Something wrong with loading the control in the form.Designer Pin
Fabrizio Stellato18-Jul-12 23:55
memberFabrizio Stellato18-Jul-12 23:55 
SuggestionRe: Something wrong with loading the control in the form.Designer Pin
MrGenie21-Feb-13 23:45
memberMrGenie21-Feb-13 23:45 
GeneralThanks. Pin
mike050527-Feb-09 10:41
membermike050527-Feb-09 10:41 
QuestionPlease help me [modified] Pin
amini_porroo7-Jan-09 10:26
memberamini_porroo7-Jan-09 10:26 
General?????? [modified] Pin
SemyonPark13-Aug-08 22:22
memberSemyonPark13-Aug-08 22:22 
QuestionExcelent article - Why using component? Pin
ajliaks21-Apr-08 20:37
memberajliaks21-Apr-08 20: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 | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 21 Feb 2006
Article Copyright 2006 by MrPJ
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid