Click here to Skip to main content
11,428,221 members (61,239 online)
Click here to Skip to main content

Enhanced BrowseForFolder styled TreeView

, 27 May 2013 Apache
Rate this:
Please Sign up or sign in to vote.
Supports Explorer, SingleChecked and RecursiveChecked mode (checkboxes). Lets you specify the displayed drive types etc...

Introduction

This tree view control gives you the ability to control which drive types are displayed to let the user choose directories. A possible scenario is to show only local drives, because your Application scan the selected directories and fill a database with the generated meta data, and it makes no sense to allow directories from removable drives.

Design

Introduction

This release has a new design which is based on a tree view which aggregates a tree view data provider interface (Strategy pattern). The goal of the new design is to provide an easy way to extend or add data providers without changing a single line of tree view code. Basically the tree view interface will not change so far, but the data providers will change their behavior and features.

TreeViewFolderBrowser class defines the following core requirements

  • DriveTypes
  • RootFolder
  • CheckboxBehaviorMode

and is responsible to manage the checkboxes and the internal selected directories list.

ITreeViewFolderBrowserDataProvider is used by a TreeViewFolderBrowser instance and is responsible to

  • retrieve the computer drives and directories
  • Imagelist which is used to assign images to the nodes created by this instance
  • ContextMenu

and can provide custom features and behavior to extend the TreeViewFolderBrowser class.

TreeViewFolderBrowser

You can specify the drive types through a public instance property on the control. The enumeration can be treated as a bit field, that is, a set of flags.

Member Name Description
NoRootDirectory NoRootDirectory
RemovableDisk Drive has removable media. This includes all floppy drives and many other varieties of storage devices.
LocalDisk Drive has fixed (nonremovable) media. This includes all hard drives, including hard drives that are removable.
NetworkDrive Network drives. This includes drives shared anywhere on a network.
CompactDisc Drive is a CD-ROM. No distinction is made between read-only and read/write CD-ROM drives.
RAMDisk Drive is a block of Random Access Memory (RAM) on the local computer that behaves like a disk drive.

The different CheckboxBehaviorMode indicates whether check boxes are displayed next to the tree nodes in the tree view control and how the tree view handle related events. The main difference between SingleChecked and RecursiveChecked behavior, lies in the fact that the user can't unselect sub folders of a checked folder in RecursiveChecked mode.

Member Name Description
None No check boxes are displayed next to the tree nodes in the tree view control.
SingleChecked Check boxes are displayed next to the tree nodes in the tree view control. The user can check directories.
RecursiveChecked Check boxes are displayed next to the tree nodes in the tree view control. The user can check directories, the subdirectories are checked recursive.

The root folder property let you specify where the browsing starts from. Root folder values are defined by System.Environment.SpecialFolder.

Member Name  Description
Desktop The tree view control shows a virtual desktop root node. Personal node points to the user my files folder. The MyComputer node shows the specified drive types.
MyComputer The tree view control shows the specified drive types on the root.
All other values from System.Environment.SpecialFolder The tree view control shows the specified root folder, the drive types are ignored

The combination of DriveType, CheckboxBehaviorModes and SpecialFolder enumeration values gives you the ability to control how the tree view display it's content and behaves when you select a directory.

DataProvider

Data providers are the workers behind the TreeViewFolderBrowser which controls them. By implementing the ITreeViewFolderBrowserDataProvider interface, you will have full control over the core processes like retrieving data, assign images to the nodes and provide custom ContextMenu items for each node. But you don't have to care about checkboxes, load on demand, find node at position if the user request the ContextMenu, you will be hooked if it's time to take some action on it. The only thing you must respect is the core functionality (DriveTypes and RootFolder) implemented by the TreeViewFolderBrowser.

To provide clean access to the handled TreeViewFolderBrowser class instance every method on the data provider interface provides an TreeViewFolderBrowserHelper class instance which lets you create nodes and give you access to the TreeViewFolderBrowser instance.

Please take a look at the two delivered standard implementations which can be found in the Raccoom.TreeViewFolderBrowser.DataProviders project.

Key features

TreeViewFolderBrowser

  • Different build in CheckboxBehaviorModes.
  • Step by step population for subdirectories.
  • Parent nodes are bold if there are selected subfolders, this helps to find selected directories in large structures.

TreeViewFolderBrowserDataProvider

  • Drive enumeration through strong typed Win32_Logicaldisk WMI class.
  • Shell32 ImageList used to retrieve Icons.
  • System.IO namespace used to retrieve directories
  • Respects the code access security features from .NET

TreeViewFolderBrowserDataProviderShell32

Inherit from TreeViewFolderBrowserDataProvider

  • Drive and directory enumeration through Shell32 interop against strong typed Win32_Logicaldisk WMI class drive types. (Does not respect .NET code access security)
  • Supports Shell32 virtual folders (non file system folders)
  • Shell32 ImageList used to retrieve Icons.
  • Functional context menu items for each shell object.
  • Tested against WinXP and Win2000.

Using the code

Before you begin make sure your project has a valid reference to the Raccoom.TreeViewFolderBrowser.dll. Go to the Toolbox window, right-click and select Customize Toolbox from the context menu. In the Customize Toolbox dialog go to the .NET Framework Components tab and select the Raccoom.TreeViewFolderBrowser.dll assembly that you just compiled. Now drop the TreeViewFolderBrowser control to your form.

This example assumes that you have created an instance of a TreeViewFolderBrowser control on a Form.

Fill data

// set standard data provider
this.myTreeView.DataSource = 
 new Raccoom.Windows.Forms.TreeViewFolderBrowserDataProvider();
// set drive types
this.myTreeView.DriveTypes
= DriveTypes.LocalDisk | DriveTypes.NetworkDrive | 
 DriveTypes.RemovableDisk |
 DriveTypes.CompactDisc;
// set checkbox behavior mode
this.myTreeView.CheckboxBehaviorMode= CheckboxBehaviorMode.SingleChecked;
// fill root level
this.myTreeView.Populate();

Event Handling

private void treeView_SelectedDirectoriesChanged(
 object sender, Raccoom.Windows.Forms.SelectedDirectoriesChangedEventArgse)
{
// determine the path which is currently added (checked) 
// or removed (unchecked)
this.statusBar1.Text = e.Path + " is now " + e.CheckState.ToString();
// display all selected path's in a listbox
this.listBox1.Items.Clear();
foreach(string s in myTreeView.SelectedDirectories)
{
this.listBox1.Items.Add(s);
} 
}

 

Remarks

This control display drive types and folders, so far so good. Removable disk's can change their medium and folders can change (new folder, delete folder) during run time, the control does not care about that.

Links

History

  • 03.30.2015 github source repository added
  • 04.03.2004 Major design changes
  • 08.02.2004 Major update
  • 02.07.2003 final release

enjoy... 

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Chris Richner
Software Developer (Senior) Zeit AG
Switzerland Switzerland
Biography
  • 1996 - 1998 PC Board PPL, HTML, DHTML, Javascript and ASP
  • 1999 - 2001 coding Centura against Sql Database (SqlBase,MSSQL,Oracle)
  • 2002 - 2004 C# Windows Forms
  • 2005 - 2006 C# ASP.NET, Windows Forms
  • 2006 - 2009 C#, WCF, WF, WPF
  • 2010 - 2012 C#, Dynamics CRM, Sharepoint, Silverlight
  • 2013 - 2013 C#, WCF DS (OData), WF, WPF
  • 2014 - now C#, Azure PaaS, Identity, OWIN, OData, Web Api
Interests
  • family & friends
  • chilaxing ,)
  • coding
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
NewsBased on popular demand I've put the latest code on github.com Pin
Chris Richner29-Mar-15 9:21
memberChris Richner29-Mar-15 9:21 
GeneralRe: Based on popular demand I've put the latest code on github.com Pin
juksranicleo29-Mar-15 10:11
memberjuksranicleo29-Mar-15 10:11 
GeneralRe: Based on popular demand I've put the latest code on github.com Pin
Chris Richner29-Mar-15 10:24
memberChris Richner29-Mar-15 10:24 
GeneralRe: Based on popular demand I've put the latest code on github.com Pin
juksranicleo31-Mar-15 11:19
memberjuksranicleo31-Mar-15 11:19 
GeneralRe: Based on popular demand I've put the latest code on github.com Pin
Chris Richner31-Mar-15 11:48
memberChris Richner31-Mar-15 11:48 
QuestionThe latest version Pin
juksranicleo29-Mar-15 6:34
memberjuksranicleo29-Mar-15 6:34 
AnswerRe: The latest version Pin
Chris Richner29-Mar-15 9:23
memberChris Richner29-Mar-15 9:23 
AnswerRecursive Check Fix Pin
Member 105678725-Feb-14 6:39
memberMember 105678725-Feb-14 6:39 
GeneralRe: Recursive Check Fix Pin
Chris Richner5-Feb-14 20:03
memberChris Richner5-Feb-14 20:03 
QuestionGreat Work Pin
FoffleSoft25-Sep-13 9:53
memberFoffleSoft25-Sep-13 9:53 
AnswerRe: Great Work Pin
Chris Richner26-Sep-13 1:15
memberChris Richner26-Sep-13 1:15 
GeneralRe: Great Work Pin
FoffleSoft26-Sep-13 8:32
memberFoffleSoft26-Sep-13 8:32 
BugIcon disappearing when Checkboxes is false Pin
clwprogrammer31-May-13 1:57
memberclwprogrammer31-May-13 1:57 
GeneralAwesome Article Pin
ashishgupta12122-Mar-13 9:26
memberashishgupta12122-Mar-13 9:26 
GeneralRe: Awesome Article Pin
Chris Richner3-Mar-13 3:09
memberChris Richner3-Mar-13 3:09 
BugException fix. Pin
Koson26-Jun-12 1:23
memberKoson26-Jun-12 1:23 
GeneralRe: Exception fix. Pin
Chris Richner24-Jul-12 0:31
memberChris Richner24-Jul-12 0:31 
SuggestionSorting of the subdirs Pin
m_kuhn11-Dec-11 3:16
memberm_kuhn11-Dec-11 3:16 
GeneralRe: Sorting of the subdirs Pin
Chris Richner15-Mar-12 1:43
memberChris Richner15-Mar-12 1:43 
GeneralMy vote of 5 Pin
DontSailBackwards23-Nov-11 11:12
memberDontSailBackwards23-Nov-11 11:12 
QuestionDe-selecting Subfolders? [modified] Pin
Member 30938448-Jun-11 6:26
memberMember 30938448-Jun-11 6:26 
AnswerRe: De-selecting Subfolders? [modified] [modified] Pin
DontSailBackwards23-Nov-11 11:17
memberDontSailBackwards23-Nov-11 11:17 
GeneralRe: De-selecting Subfolders? [modified] Pin
Chris Richner24-Nov-11 0:09
memberChris Richner24-Nov-11 0:09 
QuestionI may seem stupid, but .. Pin
Nigel Mackay20-Jun-10 22:32
memberNigel Mackay20-Jun-10 22:32 
GeneralI wonder How did you add the reference named "TreeViewExplorer_Source" in Raccoom.TreeViewFolderBrowser.Demo Pin
ic_nim15-Feb-10 8:18
memberic_nim15-Feb-10 8:18 
I'm very very new to C#.After I have study your code,I notice that It's under Raccoom.TreeViewFolderBrowser.dll. However, I don't know how did you add it to the project since it's inside the dll again, or I'm just too new for C#.
GeneralWorks on Vista, not on XP Pin
scotchfaster29-Nov-09 19:36
memberscotchfaster29-Nov-09 19:36 
GeneralRe: Works on Vista, not on XP Pin
Chris Richner30-Nov-09 7:24
memberChris Richner30-Nov-09 7:24 
GeneralRe: Works on Vista, not on XP Pin
scotchfaster21-Dec-09 16:31
memberscotchfaster21-Dec-09 16:31 
GeneralMulti Columns Pin
ddurnan22-Oct-09 2:36
memberddurnan22-Oct-09 2:36 
GeneralContext menu Pin
Phazonmutant10-Aug-09 14:13
memberPhazonmutant10-Aug-09 14:13 
GeneralRe: Context menu Pin
Chris Richner10-Aug-09 21:27
memberChris Richner10-Aug-09 21:27 
Generalpopulate with List<string> (or StringCollection) of paths</string> Pin
Saso Tepina2-Apr-09 19:47
memberSaso Tepina2-Apr-09 19:47 
GeneralRe: populate with List (or StringCollection) of paths Pin
Chris Richner2-Apr-09 22:50
memberChris Richner2-Apr-09 22:50 
GeneralRe: populate with List (or StringCollection) of paths [modified] Pin
Saso Tepina2-Apr-09 23:22
memberSaso Tepina2-Apr-09 23:22 
GeneralRe: populate with List (or StringCollection) of paths Pin
Chris Richner3-Apr-09 0:21
memberChris Richner3-Apr-09 0:21 
GeneralRe: populate with List (or StringCollection) of paths Pin
Saso Tepina3-Apr-09 0:39
memberSaso Tepina3-Apr-09 0:39 
GeneralRe: populate with List (or StringCollection) of paths Pin
Saso Tepina3-Apr-09 1:35
memberSaso Tepina3-Apr-09 1:35 
GeneralRe: populate with List (or StringCollection) of paths Pin
Chris Richner3-Apr-09 6:42
memberChris Richner3-Apr-09 6:42 
GeneralRe: populate with List (or StringCollection) of paths Pin
Saso Tepina3-Apr-09 7:31
memberSaso Tepina3-Apr-09 7:31 
GeneralRe: populate with List (or StringCollection) of paths Pin
Chris Richner5-Apr-09 22:21
memberChris Richner5-Apr-09 22:21 
GeneralFix for a little bug (all subfolders checked if you first check and then expand) Pin
Saso Tepina2-Apr-09 17:17
memberSaso Tepina2-Apr-09 17:17 
General"Failed to get icon index" on Vista Pin
DenXX19-Feb-09 14:36
memberDenXX19-Feb-09 14:36 
GeneralRe: "Failed to get icon index" on Vista Pin
Chris Richner20-Feb-09 0:03
memberChris Richner20-Feb-09 0:03 
GeneralRe: "Failed to get icon index" on Vista Pin
kingjotte1-Apr-09 23:04
memberkingjotte1-Apr-09 23:04 
GeneralVery nice work Pin
imadulhaq14-Jan-09 3:56
memberimadulhaq14-Jan-09 3:56 
GeneralRe: Very nice work Pin
Chris Richner14-Jan-09 8:20
memberChris Richner14-Jan-09 8:20 
GeneralRe: Very nice work Pin
imadulhaq14-Jan-09 19:14
memberimadulhaq14-Jan-09 19:14 
GeneralRe: Very nice work Pin
Chris Richner15-Jan-09 4:45
memberChris Richner15-Jan-09 4:45 
GeneralRemovable harddisks are displayed even when selected drivetype is LocalDisk Pin
dkmittal3-Nov-08 21:25
memberdkmittal3-Nov-08 21:25 
GeneralRe: Removable harddisks are displayed even when selected drivetype is LocalDisk Pin
Chris Richner4-Nov-08 6:27
memberChris Richner4-Nov-08 6:27 

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
Web04 | 2.8.150428.2 | Last Updated 27 May 2013
Article Copyright 2003 by Chris Richner
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid