We often need to have users use a "folder browser" dialog in our applications. You surely already have used the Win32 ShBrowseForFolder function when building VB6 applications? Guess what, Microsoft Visual Studio .Net team forgot this feature when they built VS.Net 2002!
There are at least two ways for handling this dialog right now. One is managed while the other is unmanaged. Unmanaged code runs outside the .Net Framework. I will show you the managed way and I will give you links for the unmanaged code.
Create a new project.
Create a new Visual Basic .Net project using the Windows Application template.
Since this project inherits an existing class, we need to add a reference. Select "Add Reference..." from the Project menu. From the "Add Reference" dialog, choose "System.design.dll" (in the .Net tab) and click the Select button and then the OK button.
Create a wrapper class.
We will create a class that wraps the functionality of the folder browser. This wrapper will ease the re-use of this feature. About 5 lines will be required to use the feature.
Select "Add Class..." from the Project menu. Be sure that the Class template is selected and change the name to MyFolderBrowser.vb. Finally, click the Open button.
Before the "Public Class MyFolderBrowser", we need to import the Design namespace using this line:
Imports System.Windows.Forms.Design
Inherits FolderNameEditor
Public Enum enuFolderBrowserFolder Desktop = FolderBrowserFolder.Desktop Favorites = FolderBrowserFolder.Favorites MyComputer = FolderBrowserFolder.MyComputer MyDocuments = FolderBrowserFolder.MyDocuments MyPictures = FolderBrowserFolder.MyPictures NetAndDialUpConnections = _ FolderBrowserFolder.NetAndDialUpConnections NetworkNeighborhood = _ FolderBrowserFolder.NetworkNeighborhood Printers = FolderBrowserFolder.Printers Recent = FolderBrowserFolder.Recent SendTo = FolderBrowserFolder.SendTo StartMenu = FolderBrowserFolder.StartMenu Templates = FolderBrowserFolder.Templates End Enum 'The FolderBrowserStyles collection is a member of FolderNameEditor Public Enum enuFolderBrowserStyles BrowseForComputer = FolderBrowserStyles.BrowseForComputer BrowseForEverything = FolderBrowserStyles.BrowseForEverything BrowseForPrinter = FolderBrowserStyles.BrowseForPrinter RestrictToDomain = FolderBrowserStyles.RestrictToDomain RestrictToFilesystem = FolderBrowserStyles.RestrictToFilesystem RestrictToSubfolders = FolderBrowserStyles.RestrictToSubfolders ShowTextBox = FolderBrowserStyles.ShowTextBox End Enum
Public StartLocation As enuFolderBrowserFolder = _ enuFolderBrowserFolder.BrowseForComputer Public Style As enuFolderBrowserStyles = _ enuFolderBrowserStyles.ShowTextBox
Private mstrDescription As String = _ "Please select a directory below:" Private mstrPath As String = String.Empty Private mobjFB As New FolderBrowser()
Public Property Description() As String Get Return mstrDescription End Get Set(ByVal Value As String) mstrDescription = Value End Set End Property
Public ReadOnly Property Path() As String Get Return mstrPath End Get End Property
Public Function ShowBrowser() As System.Windows.Forms.DialogResult With mobjFB .Description = mstrDescription .StartLocation = CType(Me.StartLocation, _ FolderNameEditor.FolderBrowserFolder) .Style = CType(Me.Style, _ FolderNameEditor.FolderBrowserStyles) Dim dlgResult As DialogResult = .ShowDialog If dlgResult = DialogResult.OK Then mstrPath = .DirectoryPath Else mstrPath = String.Empty End If Return dlgResult End With End Function
Test the class.
When we started the wrapper class, I told you that the use of the class should take about 5 lines of code. It is now the time to count them!
Add a button to the form that is already created in your project. Double-click on it to create the Click handler procedure. In the handler, copy these lines:
Dim objFB As New MyFolderBrowser() If objFB.ShowBrowser = DialogResult.OK Then MessageBox.Show(objFB.Path) End If
You can run the project now and you should see the dialog to the right appear after you have clicked the button.
Figure 1: Your Folder Browser dialog
If you select one folder and click the OK button, the selected path will be displayed into a message box (because this is what we are doing with the Path property).
Do you remember that we have added properties to our class?
Before calling the ShowBrowser method, we could have used them like this:
objFB.Description = "Type your own description" objFB.StartLocation = _ MyFolderBrowser.enuFolderBrowserFolder.Desktop objFB.Style = _ MyFolderBrowser.enuFolderBrowserStyles.ShowTextBox
What if I really want to use unmanaged code?.
You can still use the ShBrowseForFolder Win32 API call if you are ready to use unmanaged code. Why would you want to use unmanaged code? One reason could be because you may set the starting folder!
If you really want to use the ShBrowseForFolder Win32 API call, you can read the Browsing for a folder article. This article also shows you how to use Win32 APIs.
There is also another example from GotDotNet.
What about C#?
For those of you doing C#, you can have a look at HOW TO: Implement a Managed Component that Wraps the Browse For Folder Common Dialog Box by Using Visual C# .NET.
Conclusion.
The next version of Visual Studio .Net (named VS.Net 2003) is due later this month. This newer version is using the .Net Framework 1.1. The new framework will expose a folder browser feature into the System.Windows.Forms.FolderBrowserDialog class.
I hope you appreciated the topic and see you next month.