Directory Picker

bghanim

Active member
Joined
Dec 5, 2006
Messages
40
Location
Abu Dhabi - UAE
Programming Experience
1-3
Hi all,

I need to show the dialog "select folder" and get the
path selected, using vb.net. The OpenFileDialog control
is useless as I want to select a folder and not a file.
 
FolderBrowserDialog is useful.
 
Problem was solved

Thank you for your help,
It was useful, but there was a problem in giving an evnt to the button OK of the FolderBrowserDialog.
This problem was solved by searching the net. Here you can see a class that will give you FolderBrowserDialog with event in the OK button.




VB.NET:
Imports System.Windows.Forms.Design
Public Class FolderBrowser
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.MyComputer
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
End Class

then you should declare object of this class:

VB.NET:
Dim folderBrowserObj As New FolderBrowser
If folderBrowserObj.ShowBrowser = DialogResult.OK Then
Me.TextBox1.Text = folderBrowserObj.Path
End If
 
Last edited by a moderator:
There is no event in your code, neither is it necessary as to how dialogs always work by returning DialogResult values - and the default FolderBrowserDialog class works just the same:
VB.NET:
Dim fb As New FolderBrowserDialog
If fb.ShowDialog() = DialogResult.OK Then
  Me.TextBox1.Text = fb.SelectedPath
End If
 
Back
Top