How to use OpenFileDialog control to select directory path.

  • Thread starter Thread starter Sakharam Phapale
  • Start date Start date
S

Sakharam Phapale

Hi All,



I want to give option to user for selecting directory, just like for
selecting file using OpenFileDialog control.



How to make it possible?

I tried it using "VisualBasic.Compatibility.VB6.DirListBox" control, but it
does not work properly.

It has following two properties,

1.SelectedItem :- which returns just folder name.

2.Path :- which always return application path

It should return path of the selected folder, but seems to be not working.

Any solution?

Thanks in Advance

Sakharam Phapale
 
Hi,

Use the folder browser dialog instead. The control is included with
vs.net 2003 here is a link to a vs.net 2002 version.

http://www.windowsforms.com/Samples/download.aspx?PageId=1&ItemId=113&tabindex=4

Ken
-------------------------
Hi All,



I want to give option to user for selecting directory, just like for
selecting file using OpenFileDialog control.



How to make it possible?

I tried it using "VisualBasic.Compatibility.VB6.DirListBox" control, but it
does not work properly.

It has following two properties,

1.SelectedItem :- which returns just folder name.

2.Path :- which always return application path

It should return path of the selected folder, but seems to be not working.

Any solution?

Thanks in Advance

Sakharam Phapale
 
If you have VB.NET 2003, you can use the BrowseForFolder dialog to do that. Otherwise, you have to do this:
Copied from a post by :Anthony Glenwright.

In VB, create a class:

Public Class FolderBrowser
Inherits System.Windows.Forms.Design.FolderNameEditor
Private objBrowse As New
System.Windows.Forms.Design.FolderNameEditor.FolderBrowser()

Sub New(ByVal Text As String)
MyBase.New()
objBrowse.Description = Text
End Sub

Function ShowDialog() As Windows.Forms.DialogResult
Return objBrowse.ShowDialog
End Function

Property Text() As String
Get
Return objBrowse.Description
End Get
Set(ByVal Value As String)
objBrowse.Description = Text
End Set
End Property

ReadOnly Property DirectoryPath() As String
Get
Return objBrowse.DirectoryPath
End Get
End Property
End Class

Then you can call it with:

Dim objBrowse As New FolderBrowser("Please select a folder")
If objBrowse.ShowDialog = DialogResult.OK Then
msgbox ("You picked " & objBrowse.DirectoryPath)
End If

This worked for me until I upgraded to VB.NET 2003. Then the BrowseForFolder Dialog control was added. But, this one works just
fine.
james
 
Back
Top