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).
Saturday, June 23, 2007
Subscribe to:
Post Comments (Atom)
Good stuff...helped a bunch.
ReplyDeleteThanks
Quick and Easy! Thanks!
ReplyDeleteThanks for making it easy to understand.
ReplyDeleteVery helpful! Thanks
ReplyDeleteSnezana
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
ReplyDeleteNice!!!
ReplyDeleteGreat! Thanks!
ReplyDeleteStill helping us out! Thanks!
ReplyDeleteMany thanks for your help.
ReplyDeleteWorks for me - thanks for putting this together.
ReplyDeletenice stuff
ReplyDeleteTks alot
ReplyDeletegrt... thanks
ReplyDeleteGreat! It worked! Thanks
ReplyDelete