Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  VB.NET >  Uncovering the .Net Framework (v 1.0) FolderBrowser Dialog
Add to MyHood
Uncovering the .Net Framework (v 1.0) FolderBrowser Dialog   [ printer friendly ]
Stats
  Rating: 4.6 out of 5 by 5 users
  Submitted: 05/15/03
Steve Menyhart ()

 
There are many people out there asking how to get access to a folder browser dialog in .Net. I've heard answers ranging from "implement your own" to "use interop to access the folder browser in the windows shell". After a lot of websurfing I finally found a reference to a protected class hidden away in the .Net Library.

System.Windows.Forms.Design.FolderNameEditor has a protected class called FolderBrowser and it turns out to be exactly what it says: The folder browser you see in any old windows application. To use it, I merely made my own class that inherited from FolderNameEditor and then exposed the methods of the protected class FolderBrowser. I had to duplicate some protected enumerations too and then tie them together for some properties.

Here's my code:

'A dialog class which displays a tree view of folders and allows the user to select a folder path
Public Class FolderBrowser
    Inherits System.Windows.Forms.Design.FolderNameEditor

    'Some folders the starting location of the FolderBrowser can be set to
    Public Enum Folder
        Desktop
        Favorites
        MyComputer
        MyDocuments
        MyPictures
        NetAndDialUpConnections
        NetworkNeighborhood
        Printers
        Recent
        SendTo
        StartMenu
        Templates
    End Enum

    'Styles effecting the look and functionality of the FolderBrowser dialog
    Public Enum BrowserStyles
        BrowseForComputer
        BrowseForEverything
        BrowseForPrinter
        RestrictToDomain
        RestrictToFileSystem
        RestrictToSubfolders
        ShowTextBox
    End Enum

    Private _browser As FolderBrowser 'the protected dialog

    Public Sub New()
        _browser = New FolderBrowser()
    End Sub

    Public Function ShowDialog() As DialogResult
        Return _browser.ShowDialog()
    End Function

    Public Function ShowDialog(ByVal owner As System.Windows.Forms.IWin32Window) As DialogResult
        Return _browser.ShowDialog(owner)
    End Function

    'The Path the user has chosen
    Public ReadOnly Property DirectoryPath() As String
        Get
            Return _browser.DirectoryPath
        End Get
    End Property

    'Puts a text string at the top of the dialog
    Public Property Description() As String
        Get
            Return _browser.Description
        End Get
        Set(ByVal Value As String)
            _browser.Description = Value
        End Set
    End Property

    Public Property StartLocation() As Folder
        Get
            Select Case _browser.StartLocation
                Case FolderBrowserFolder.Desktop
                    Return Folder.Desktop
                Case FolderBrowserFolder.Favorites
                    Return Folder.Favorites
                Case FolderBrowserFolder.MyComputer
                    Return Folder.MyComputer
                Case FolderBrowserFolder.MyDocuments
                    Return Folder.MyDocuments
                Case FolderBrowserFolder.MyPictures
                    Return Folder.MyPictures
                Case FolderBrowserFolder.NetAndDialUpConnections
                    Return Folder.NetAndDialUpConnections
                Case FolderBrowserFolder.NetworkNeighborhood
                    Return Folder.NetworkNeighborhood
                Case FolderBrowserFolder.Printers
                    Return Folder.Printers
                Case FolderBrowserFolder.Recent
                    Return Folder.Recent
                Case FolderBrowserFolder.SendTo
                    Return Folder.SendTo
                Case FolderBrowserFolder.StartMenu
                    Return Folder.StartMenu
                Case FolderBrowserFolder.Templates
                    Return Folder.Templates
            End Select
        End Get
        Set(ByVal Value As Folder)
            Select Case Value
                Case Folder.Desktop
                    _browser.StartLocation = FolderBrowserFolder.Desktop
                Case Folder.Favorites
                    _browser.StartLocation = FolderBrowserFolder.Favorites
                Case Folder.MyComputer
                    _browser.StartLocation = FolderBrowserFolder.MyComputer
                Case Folder.MyDocuments
                    _browser.StartLocation = FolderBrowserFolder.MyDocuments
                Case Folder.MyPictures
                    _browser.StartLocation = FolderBrowserFolder.MyPictures
                Case Folder.NetAndDialUpConnections
                    _browser.StartLocation = FolderBrowserFolder.NetAndDialUpConnections
                Case Folder.NetworkNeighborhood
                    _browser.StartLocation = FolderBrowserFolder.NetworkNeighborhood
                Case Folder.Printers
                    _browser.StartLocation = FolderBrowserFolder.Printers
                Case FolderBrowserFolder.Recent
                    _browser.StartLocation = FolderBrowserFolder.Recent
                Case Folder.SendTo
                    _browser.StartLocation = FolderBrowserFolder.SendTo
                Case Folder.StartMenu
                    _browser.StartLocation = FolderBrowserFolder.StartMenu
                Case Folder.Templates
                    _browser.StartLocation = FolderBrowserFolder.Templates
            End Select
        End Set
    End Property

    Public Property Style() As BrowserStyles
        Get
            Select Case _browser.Style
                Case FolderBrowserStyles.BrowseForComputer
                    Return BrowserStyles.BrowseForComputer
                Case FolderBrowserStyles.BrowseForEverything
                    Return BrowserStyles.BrowseForEverything
                Case FolderBrowserStyles.BrowseForPrinter
                    Return BrowserStyles.BrowseForPrinter
                Case FolderBrowserStyles.RestrictToDomain
                    Return BrowserStyles.RestrictToDomain
                Case FolderBrowserStyles.RestrictToFilesystem
                    Return BrowserStyles.RestrictToFileSystem
                Case FolderBrowserStyles.RestrictToSubfolders
                    Return BrowserStyles.RestrictToSubfolders
                Case FolderBrowserStyles.ShowTextBox
                    Return BrowserStyles.ShowTextBox
            End Select
        End Get
        Set(ByVal Value As BrowserStyles)
            Select Case Value
                Case BrowserStyles.BrowseForComputer
                    _browser.Style = FolderBrowserStyles.BrowseForComputer
                Case BrowserStyles.BrowseForEverything
                    _browser.Style = FolderBrowserStyles.BrowseForEverything
                Case BrowserStyles.BrowseForPrinter
                    _browser.Style = FolderBrowserStyles.BrowseForPrinter
                Case BrowserStyles.RestrictToDomain
                    _browser.Style = FolderBrowserStyles.RestrictToDomain
                Case BrowserStyles.RestrictToFileSystem
                    _browser.Style = FolderBrowserStyles.RestrictToFilesystem
                Case BrowserStyles.RestrictToSubfolders
                    _browser.Style = FolderBrowserStyles.RestrictToSubfolders
                Case BrowserStyles.ShowTextBox
                    _browser.Style = FolderBrowserStyles.ShowTextBox
            End Select
        End Set
    End Property

End Class


This should be slightly easier to use than trying to implement your own or setting up interop, although it has a few limitations like only being able to set the starting folder to a few predifined places. I realize after writing that that the new version of the framework has a readily accessable FolderBrowser Dialog now so I wish I had found and posted this solution earlier. It might still be of use to people not ready to update to the new version of the framework (1.1) or people that would like to keep their code compatible to be run on the old framework (1.0). Hope you enjoy :)

Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
.NET Framework 1.1 put back in the folder browser dialog class.
-- Victor Vuong, June 07, 2003
 
Yeah I noticed that 1.1 has a folderbrowser, as I mentioned at the bottom of the tutorial, but thought it might still be usefull.
-- Steve Menyhart, June 09, 2003
 
Wow, I'm impressed! I looked all over the place for a folder browser and ended up just using an OpenFileDialog hack. That's great that you found one and posted it. I also didn't know there was one know avalible in v1.1, once again, thanks!
-- Noah Coad, June 27, 2003
 
This is good to know. Ive been looking for something like this as well, thanks =)
-- Ben Ratzlaff, July 14, 2003
 
Copyright © 2001 DevHood® All Rights Reserved