mike mcintyre's

.N e t J o u r n a l

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AprMay 2008Jun
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Topics

Source Code

Source Code: How To Browse Folders with FolderBrowserDialog and Visual Basic 2005

How To Browse Folders with FolderBrowserDialog and Visual Basic 2005

This article and source code demonstrate how to use the .NET Windows Forms FolderBrowserDialog class and Visual Basic 2005 to provide a way to prompt the user to browse, create, and eventually select a folder. Use this class when you only want to allow the user to select folders, not files.

 

Browsing of the folders is done through a tree control. Only folders from the file system can be selected; virtual folders cannot.
 

After creating a new FolderBrowserDialog, you can set the RootFolder to the location from which to start browsing. You can also set the SelectedPath to an absolute path of a subfolder of RootFolder that will initially be selected.

 

You can also optionally set the Description property to provide additional instructions to the user. Finally, call the ShowDialog method to display the dialog box to the user.

 

When the dialog box is closed and the dialog box result from ShowDialog is DialogResult.OK, the SelectedPath will be a string containing the path to the selected folder.


You can use the ShowNewFolderButton property to control if the user is able to create new folders with the New Folder button.
 

FolderBrowserDialog is a modal dialog box; therefore, when shown, it blocks the rest of the application until the user has chosen a folder. When a dialog box is displayed modally, no input (keyboard or mouse click) can occur except to objects on the dialog box. The program must hide or close the dialog box (usually in response to some user action) before input to the calling program can occur.
 

Example One - Browse for and select a folder.

 

Private Sub BrowseFoldersButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseFoldersButton.Click

 

    ' Declare a variable named theFolderBrowser of type FolderBrowserDialog.

    Dim theFolderBrowser As New FolderBrowserDialog

 

    ' Set theFolderBrowser object's Description property to

    '   give the user instructions.

    theFolderBrowser.Description = "Please select a folder for the download."

 

    ' Set theFolderBrowser object's ShowNewFolder property to false when

    '   the a FolderBrowserDialog is to be used only for selecting an existing folder.

    theFolderBrowser.ShowNewFolderButton = False

 

    ' Optionally set the RootFolder and SelectedPath properties to

    '   control which folder will be selected when browsing begings

    '   and to make it the selected folder.

    ' For this example start browsing in the Desktop folder.

    theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop

    ' Default theFolderBrowserDialog object's SelectedPath property to the path to the Desktop folder.

    theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop

 

    ' If the user clicks theFolderBrowser's OK button..

    If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then

        ' Set the FolderChoiceTextBox's Text to theFolderBrowserDialog's

        '    SelectedPath property.

        Me.FolderChoiceTextBox.Text = theFolderBrowser.SelectedPath

    End If

 

 End Sub

 

Example Two - Find a folder and create a new folder within it.

 

Private Sub NewFolderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewFolderButton.Click

 

    ' Declare a variable named theFolderBrowser of type FolderBrowserDialog.

    Dim theFolderBrowser As New FolderBrowserDialog

 

    ' Set theFolderBrowser object's Description property to

    '   the give the user instructions.

    theFolderBrowser.Description = "Please create a folder for the download. After you create the folder, select it and press the OK button."

 

    ' DO NOT SET theFolderBrowser object's ShowNewFolder property to false when

    '   the a FolderBrowserDialog is to be used to create a new folder.

 

    ' Optionally set the RootFolder and SelectedPath properties to

    '   control which folder will be selected when browsing begings

    '   and to make it the selected folder.

    ' For this example start browsing in the user's MyPictures directory.

    theFolderBrowser.RootFolder = System.Environment.SpecialFolder.MyPictures

    ' Default theFolderBrowserDialog object's SelectedPath property to the path to the MyPictures folder.

    theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyPictures

 

    ' If the user clicks theFolderBrowser's OK button..

    If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then

        ' Set the FolderChoiceTextBox's Text to theFolderBrowserDialog's

        '    SelectedPath property.

        Me.FolderChoiceTextBox.Text = theFolderBrowser.SelectedPath

    End If

 

End Sub

 

For more information click:  FolderBrowserDialog

 

mike mcintyre http://www.getdotnetcode.com

Subscribe to GetDotNetCode's How To News Feed

posted on Monday, January 08, 2007 8:21 PM