Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C#
Article

Folder Browser component for .NET

Rate me:
Please Sign up or sign in to vote.
4.63/5 (27 votes)
5 Mar 2002CPOL2 min read 427.5K   2.8K   80   64
A component that provides shell folder browsing dialog to .NET applications

Demo Application

Folder browser demo

Dialog generated by settings in the demo application

Folder browser demo

Introduction

ShellFolderBrowser
is a component that makes it possible to use shell's folder browsing dialog for .NET applications. It can be used in the same way as
OpenFileDialog
and SaveFileDialog components which are available from the framework SDK. The component makes use of SHBrowseForFolder function and demonstrates various aspects of interoperability of managed and unmanaged code like passing delegates function pointers, passing structures and strings.

It is quite simple to use the component. You can place it on the toolbox and drag it to the form. You can set the desired properties in the component in the property grid as shown :-


Folder browser demo

In order to display the dialog all that needs to be done is to call the

ShowDialog
method as

folderBrowser1.ShowDialog();

Here is a brief description of the component. For detailed description see the documentation of SHBrowseForFolder.

Properties

BrowseFlagsSets the flags that control the behaviour of the dialog
FolderDisplayNameThe display name of the folder selected by the user
FolderPathThe folder path that was selected
TitleString that is displayed above the tree view control in the dialog box. This string can be used to specify instructions to the user. Can only be modified if the dalog is not currently displayed.

Methods

EnableOKButtonEnables or disables the ok button in the dialog
SetExpandedExpand a path in the folder
SetOkButtonTextSets the text of the OK button in the dialog
SetSelectionSets the selection the text specified
SetStatusTextSets the text of the staus area of the folder dialog
ShowDialogShows the dialog

Events

InitializedFired when the dialog is initialized (when BFFM_INITIALIZED is send to the
BrowseCallback
function)
IUnknownObtainedShell provides an IUnknown through this event. For details see documentation of
SHBrowseForFolder
(when BFFM_INITIALIZED is send to the BrowseCallback function)
SelChangedFired when selection changes (when BFFM_INITIALIZED is send to the
BrowseCallback
function)
ValidateFailedFired when validation of text typed by user fails (when BFFM_INITIALIZED is send to the BrowseCallback function)

BrowseFlags 

BrowseFlags is an enumeration with Flags attribute set and can be a combination of any of the following values :-

ReturnOnlyFSDirs

Same as BIF_RETURNONLYFSDIRS

DontGoBelowDomain

Same as BIF_DONTGOBELOWDOMAIN

ShowStatusText

Same as BIF_STATUSTEXT

ReturnFSancestors

Same as BIF_RETURNFSANCESTORS

EditBox

Same as BIF_EDITBOX

Validate

Same as BIF_VALIDATE

NewDialogStyle

Same as BIF_NEWDIALOGSTYLE

BrowseIncludeURLs

Same as BIF_BROWSEINCLUDEURLS

AddUsageHint

Same as BIF_UAHINT

NoNewFolderButton

Same as BIF_NONEWFOLDERBUTTON

BrowseForComputer

Same as BIF_BROWSEFORCOMPUTER

BrowseForPrinter

Same as BIF_BROWSEFORPRINTER

IncludeFiles

Same as BIF_BROWSEINCLUDEFILES

ShowShareable

Same as BIF_SHAREABLE

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Creating a component that does the same - a TreeView component Pin
chriskoiak8-Jan-04 2:04
chriskoiak8-Jan-04 2:04 
GeneralRe: Creating a component that does the same - a TreeView component Pin
jmcc2k11-Nov-08 17:46
jmcc2k11-Nov-08 17:46 
GeneralCouldn't set selected Pin
6-Mar-02 16:09
suss6-Mar-02 16:09 
GeneralRe: Couldn't set selected Pin
Rama Krishna Vavilala8-Mar-02 7:05
Rama Krishna Vavilala8-Mar-02 7:05 
GeneralRe: Couldn't set selected Pin
JDutch2-Jul-02 5:05
JDutch2-Jul-02 5:05 
Generalnot very .net'ish Pin
6-Mar-02 7:36
suss6-Mar-02 7:36 
GeneralRe: not very .net'ish Pin
Rama Krishna Vavilala6-Mar-02 8:09
Rama Krishna Vavilala6-Mar-02 8:09 
GeneralRe: not very .net'ish Pin
6-Mar-02 9:40
suss6-Mar-02 9:40 
Well lesse
I think the BrowseFlags property should be split into a handful of properties, and maybe use your enum internally to make the final call. Try to make the the property name meanings more obvious, c++ style terseness and abbreviations are not suggested for public interfaces.

ShowDialog should return a DialogResult, not a Boolean

the SetXxx methods should be wrapped with properties

I don't know what the IUnknownObtained event is for, but it's obviously a very COM specific event, that maybe should be wrapped with what .net apps will want to do with it.

SelChanged should be expanded to SelectionChanged, there's no need for shortened, cryptic names for members. this is especially true of events.

a few of your event arguments have IntPtr's and IUnknown references. remove them. those types of things should be wrapped internally so your .net component provides a COM-free and Pointer-free interface.

you mentioned the CommonDialog components in your reply, and I say that you should note that the public interface for all the CommonDialog controls have absolutely no public interfaces that expose COM or Win32 api. ( except, of course, for the IWin32Window ShowDialog overload ( which i'm not really happy with, btw ) ).

It's this cleaning up of the api to a more sane interface that is one of the strong points of the Windows.Forms namespace, and it's also one of the reasons it took so long for MS to get it right.

Please don't take this to mean I don't respect what you've done. I just think that the .net framework raises the bar a bit for API design, and we'd do well to study and emulate it.
GeneralRe: not very .net'ish Pin
Rama Krishna Vavilala6-Mar-02 9:43
Rama Krishna Vavilala6-Mar-02 9:43 
GeneralRe: not very .net'ish Pin
Christopher Lord24-Oct-02 16:25
Christopher Lord24-Oct-02 16:25 

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.