Windows Phone Developers

Saturday, June 23, 2007

Selecting a Folder in VB.Net

Directory Selection in Windows Forms using VB.Net

The Visual Basic 6.0 DirListBox control has been rendered obsolete by the OpenFileDialog and SaveFileDialog components in Visual Basic 2005. If you want to select a directory FolderBrowser Dialog will be the one you need to use

For this example, Let us have a form with a TextBox and a command button. When the command buton is pressed, the folderdialog is shown and then the selected folder is displayed in the textbox

Sample Form:




Add the FolderBrowser Dialog to the form from the Dialogs Collection (see below).




This control will not be placed on the form but on a separate tray at the bottom of the Windows Forms Designer. (see below)





Now in the click event for the Button have the following code:

Private Sub BtnFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFolder.Click

Dim MyFolderBrowser As New System.Windows.Forms.FolderBrowserDialog

' Descriptive text displayed above the tree view control in the dialog box
MyFolderBrowser.Description = "Select the Folder"

' Sets the root folder where the browsing starts from
'MyFolderBrowser.RootFolder = Environment.SpecialFolder.MyDocuments

' Do not show the button for new folder
MyFolderBrowser.ShowNewFolderButton = False

Dim dlgResult As DialogResult = MyFolderBrowser.ShowDialog()

If dlgResult = Windows.Forms.DialogResult.OK Then
txt_watchpath.Text = MyFolderBrowser.SelectedPath
End If

End Sub


The FolderBrowserDialog component is displayed at run time using the ShowDialog method. Set the RootFolder property to determine the top-most folder and any subfolders that will appear within the tree view of the dialog box. Once the dialog box has been shown, you can use the SelectedPath property to get the path of the folder that was selected


For similar functionality in VB6.0 refer http://vbadud.blogspot.com/2007/04/browse-folder-select-folder-thru-shell.html

When a Visual Basic 6.0 application is upgraded to Visual Basic 2005, any existing DirListBox controls are upgraded to the VB6.DirListBox control that is provided as a part of the compatibility library (Microsoft.VisualBasic.Compatibility). Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

14 comments:

  1. Good stuff...helped a bunch.
    Thanks

    ReplyDelete
  2. Thanks for making it easy to understand.

    ReplyDelete
  3. Very helpful! Thanks
    Snezana

    ReplyDelete
  4. Hi, thanks the code confirmed taht I am using the function correctly, but I can see any mapped network drives do you ahve the same problem? or am I missing something

    ReplyDelete
  5. Still helping us out! Thanks!

    ReplyDelete
  6. Many thanks for your help.

    ReplyDelete
  7. Works for me - thanks for putting this together.

    ReplyDelete
  8. Great! It worked! Thanks

    ReplyDelete