Menu

FolderBrowserDialogEx- FolderBrowserDialog’s big brother.

April 5, 2015 - Programming

.NET and Windows Forms includes “FolderBrowserDialog” this is the common Directory browser dialog we’ve come to expect.

However the implementation leaves out a lot of the features of the underlying API. it doesn’t handle events, doesn’t support the new UI style, doesn’t allow for a edit box or status bar, etc. This is rather disappointing since my own VB6 implementation managed to do all these things. Since I’ve got a few programs that make use of the dialog and found myself- as a user of those applications- annoyed at the usability drop with the dialog, I set about creating an expanded version.

My original attempts went sour quite quickly… But I managed to sort the issue out in the last week or so.

SHBrowseForFolder

The underlying API of the FolderBrowserDialog is the SHBrowseForFolder API Function. This function is similar in design to other functions such as the GetOpenFilename and GetSaveFilename functions used by the OpenFileDialog and SaveFileDialog respectively (which both appear to lack support for their various notifications and messages…). You create a Structure and populate it, then send it to the function, then use the structure to get the result of the function call.

That is sufficient for basic usage of the function. However if you want to use advanced features- in particular, if you want any capabilities provided by using the callback, things get trickier. As I mentioned above, I found my original attempts to use the callback were hit or miss- with misses being more common. Typically, the problem manifested as a InvalidOperationException occurring in an unknown module, which made it difficult to really track down the issue. I was able to identify the issue as occurring within the callback function itself when the callback attempted to convert the IDList to a string path to fire an event. After fiddling with it for a while I managed to find some C/C++ examples of use of the bare API. They seemed to include uses of interfaces such as IMalloc that I hadn’t used, so I scrapped what I had and instead tried to port those implementations to C#. The result was a usable class that provides the full capabilities (I think) of the SHBrowseForFolder API Function, ready for me to insert as nearly a drop-in replacement for the BrowseFolderDialog.

I could explore this domain piecemeal- provide a basic working component, then draw it out with blog entries for each additional feature. Arguably, not a bad idea, but I cannot be trusted doing that, and realistically the version that would best benefit people would be the complete version- so here it is:

And of course any code sample like this is not complete without at least a basic usage example. the class contains a static method that can be called directly for more straightforward usage, or the standard pattern of creating the class, setting properties, and using ShowDialog() can also be used:

I’m considering further additions such as trying to implement checkboxes, though I’m not sure how safe such a feature could be since it would require directly manipulating the treeview control and giving it the checkboxes style.

Have something to say about this post? Comment!