Skip to content

ArsenShnurkov/TreeViewFolderBrowser

 
 

Repository files navigation

TreeViewFolderBrowser

Supports Explorer, SingleChecked and RecursiveChecked mode (checkboxes). Lets you specify the displayed drive types etc..

Demo Application

Based on popular demand in the discussion area I've decided to put the latest code out in the wild. The software is provided "as is". Bear in mind that this is an early 3.0.0 Version which never made it's way to codeproject.com.

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 aTreeViewFolderBrowser 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 theTreeViewFolderBrowser 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 NameDescription
**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 NameDescription
**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 bySystem.Environment.SpecialFolder.

Member NameDescription
**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 ofDriveType,CheckboxBehaviorModes andSpecialFolder 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 theTreeViewFolderBrowser which controls them. By implementing theITreeViewFolderBrowserDataProvider interface, you will have full control over the core processes like retrieving data, assign images to the nodes and provide customContextMenu items for each node. But you don't have to care about checkboxes, load on demand, find node at position if the user request theContextMenu, 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 theTreeViewFolderBrowser.

To provide clean access to the handledTreeViewFolderBrowser class instance every method on the data provider interface provides anTreeViewFolderBrowserHelper class instance which lets you create nodes and give you access to theTreeViewFolderBrowser instance.

Please take a look at the two delivered standard implementations which can be found in theRaccoom.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 typedWin32_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 typedWin32_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.

About

Supports Explorer, SingleChecked and RecursiveChecked mode (checkboxes). Lets you specify the displayed drive types etc..

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%