Click here to Skip to main content
13,353,124 members (64,710 online)
Click here to Skip to main content
Add your own
alternative version

Stats

327.4K views
3.9K downloads
126 bookmarked
Posted 1 Oct 2002

FolderTreeView Control

, 25 Oct 2002
Rate this:
Please Sign up or sign in to vote.
A simple explorer-like FolderTreeView control for C#

Sample Image

Introduction

This is an all-new version of the FolderTreeView control I posted here at CodeProject some weeks ago. The control now starts in the Desktop namespace, and a new drilling method has been added so the startup folder can be specified. Please note that this control is not intended to have all of the functionality of the actual Windows Explorer TreeView - it is a light-weight control designed for use in projects where you want to supply a treeview for folder navigation, without supporting windows shell extensions. If you are looking for a control that supports shell extensions you should be looking at the excellent ËxplorerTreeControl submitted by Carlos H Perez here at the CodeProject website.

How it works

After calling the InitFolderTreeView() method, a dummy Desktop not is created as the root node, and then the shell's Desktop namespace is iterated to populate the first level nodes. The child nodes are then iterated, checking only for the presence of sub-child nodes. If sub-child nodes are found, a dummy node is inserted so that the [+] will be displayed for folders that can be expanded. When the BeforeExpand event fires, the folder being expanded is checked for a dummy node. If a dummy is present, it is removed and the node's children are re-populated. If the node's children have already been populated no further action occurs.

Usage

The FolderTreeView control is based on 3 classes:

  • FolderTreeView - The FolderTreeView implementation
  • ShellOperations - Supporting Shell Operations
  • ExtractIcons - Shell Icon Extractor
The control naturally derives from the System.Windows.Forms.TreeView, exposing 1 new property, GetSelectedNodePath() which returns the currently selected node's full path. There are 2 new methods: InitFolderTreeView() must be called to populate the TreeView - normally you would call this in your Form's OnLoad event. The 2nd method is DrillToFolder(string folderPath) - this will recurse through the tree looking for the specified folder path, returning a boolean to indicate if the search was sucessful or not.

The method used to retreive the icon for the Desktop root node works fine, but is perhaps not the best way to go about it. This control has NOT been tested in Win9x, NT or 2000 - feedback on these OS's would be much appreciated. Of course if you improve this control I would appreciate the update, and if you have any feedback to offer, please post it below.

Updates

Version 1.11:

  • Updated the GetDesktopIcon() method so it returns the shell's small desktop icon rather than the large version
  • Added code so that the Desktop node would return the full desktop folder path
I have now tested the code on Windows Me without problem - feeback on how it works with other OS's would be appreciated!

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

Furty
Web Developer
Thailand Thailand
Furty will code for food.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralRe: Windows XP shadow Pin
dgentils8-Mar-04 18:03
memberdgentils8-Mar-04 18:03 
GeneralRe: Windows XP shadow Pin
a020912926-Mar-04 13:17
membera020912926-Mar-04 13:17 
GeneralRe: Windows XP shadow Pin
dgentils26-Mar-04 14:12
memberdgentils26-Mar-04 14:12 
GeneralRe: Windows XP shadow Pin
a020912927-Mar-04 8:32
membera020912927-Mar-04 8:32 
GeneralRe: Windows XP shadow Pin
silas_yao28-Sep-04 6:28
membersilas_yao28-Sep-04 6:28 
GeneralFile Names Pin
jpadova19-Feb-04 8:07
memberjpadova19-Feb-04 8:07 
GeneralRe: File Names Pin
IndyJason792-Jan-05 19:02
memberIndyJason792-Jan-05 19:02 
GeneralTreeViewCancelEventHandler behavior Pin
Marc Clifton17-Aug-03 16:16
editorMarc Clifton17-Aug-03 16:16 
Hello again!

OK, this is a wierd one. I've set up the tree view control as part of a tab page, namely the first tab page. Everything was working fine until I added a tab page before the page with the tree view.

In the first case, the event handler fires FIRST when the form.Show() occurs, then my code drills down to a path.

Once I added the new tab, the event doesn't fire, which screws up my drill down call. The event fires ONLY when the tab page finally gets focus.

Good grief. Any ideas on how to get things properly initialized without relying on the focus mechanism? I'm a bit clueless right now.

[edit]

My only solution is to track the state of the ...Cancel event:
private void TreeViewBeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
	this.BeginUpdate();
	ShellOperations.ExpandBranch(e.Node, this.ImageList, showFoldersOnly);
	this.EndUpdate();

	// This kludge handles cases where the tree view doesn't fire this event before
	// a request to drill to a path occurs.  This happens any time the tree view is
	// placed in a non-visible area, such as a tab page that isn't immediately visible
	// when the application starts up.
	if (beforeExpandFired==false)
	{
		beforeExpandFired=true;
		if (drillToFolder != null)
		{
			DrillToFolder(drillToFolder);
		}
	}
}

public bool DrillToFolder(string folderPath)
{
	// if the initial TreeViewCancelEvent hasn't fired yet, hold off on the
	// drill to folder until it has.
	if (!beforeExpandFired)
	{
		drillToFolder=folderPath;
		return false;
	}
        ...
}


[/edit]

Marc

STL, a liability factory - Anonymously
A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr

Latest AAL Article
<a href="http://www.blogstudio.com/aal"My blog

GeneralRe: TreeViewCancelEventHandler behavior Pin
Furty17-Aug-03 19:47
memberFurty17-Aug-03 19:47 
GeneralRe: TreeViewCancelEventHandler behavior Pin
Marc Clifton18-Aug-03 3:26
editorMarc Clifton18-Aug-03 3:26 
GeneralRe: TreeViewCancelEventHandler behavior Pin
John McPherson26-Aug-03 17:17
memberJohn McPherson26-Aug-03 17:17 
GeneralPerformance and Shell32.dll questions Pin
Marc Clifton3-Aug-03 8:32
editorMarc Clifton3-Aug-03 8:32 
GeneralRe: Performance and Shell32.dll questions Pin
Furty3-Aug-03 15:46
memberFurty3-Aug-03 15:46 
GeneralRe: Performance and Shell32.dll questions Pin
Furty3-Aug-03 17:29
memberFurty3-Aug-03 17:29 
GeneralRe: Performance and Shell32.dll questions Pin
Marc Clifton9-Aug-03 12:16
editorMarc Clifton9-Aug-03 12:16 
GeneralRe: Performance and Shell32.dll questions Pin
Spunk15-Sep-03 6:17
memberSpunk15-Sep-03 6:17 
GeneralRe: Performance and Shell32.dll questions Pin
Daniël Pelsmaeker21-Sep-03 0:55
memberDaniël Pelsmaeker21-Sep-03 0:55 
GeneralRe: Performance and Shell32.dll questions Pin
jgauthier23-Sep-03 11:04
memberjgauthier23-Sep-03 11:04 
GeneralRe: Performance and Shell32.dll questions Pin
Daniël Pelsmaeker23-Sep-03 11:17
memberDaniël Pelsmaeker23-Sep-03 11:17 
GeneralRe: Performance and Shell32.dll questions Pin
IndyJason791-Jan-05 22:46
memberIndyJason791-Jan-05 22:46 
GeneralDesign View Pin
JPark31-Jul-03 23:12
memberJPark31-Jul-03 23:12 
GeneralMistakes in: Getting the FolderItem... Pin
ittay ophir22-Jul-03 4:43
memberittay ophir22-Jul-03 4:43 
GeneralGetting the FolderItem Object From the Folder Interface Pin
ittay ophir21-Jul-03 1:38
memberittay ophir21-Jul-03 1:38 
GeneralRe: Getting the FolderItem Object From the Folder Interface Pin
Furty21-Jul-03 12:02
memberFurty21-Jul-03 12:02 
GeneralFolderTreeView lists the zip-files on my desktop Pin
Terje Krång10-Jun-03 4:54
memberTerje Krång10-Jun-03 4:54 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 26 Oct 2002
Article Copyright 2002 by Furty
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid