Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browse for folder in vb.net ??

Status
Not open for further replies.

KPharm

Programmer
Feb 1, 2001
38
US
How does one get the pathname only back from a dialog? I've seen tons of code to do it in vb6 but I can't get that code to work in .net. I can't believe that MS didn't make a function for this yet. Does anyone know different or have a way of performing this?

Thanks
 
There is a way to do this but Microsoft really should have added the "Folder Only" option to the "OpenFileDialog"

Here's how I was able to do it (With the help of a post on another site)

First you have to add two references to your project

(Click on Project->Add Reference...)
under the .NET tab add these two references

System.Windows.Forms.Dll
and
System.Design.Dll

Next create a new class and name it BrowseForFolder (BrowseForFolder is what I chose to name it, you can name it whatever you want)

copy this code into the BrowseForFolder Class


Public Class BrowseForFolder
Inherits System.Windows.Forms.Design.FolderNameEditor
Dim bDialog As New FolderBrowser()
Public Function BrowseDialog(ByVal sTitle As String)
With bDialog
.Style = FolderBrowserStyles.BrowseForEverything
.StartLocation = FolderBrowserFolder.Desktop
.Description = sTitle
.ShowDialog()
BrowseDialog = .DirectoryPath()
End With
End Function
End Class



Now comes the easy part.

In your form use the code like this

Private Sub BrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Browse1.Click
Dim MyFolder As String
Dim myDialog As New BrowseForFolder()
MyFolder = myDialog.BrowseDialog("This text will be displayed at the top of the browse dialog. Change this to whatever you want")
End Sub


That's it. Here when the Button is clicked it opens a browse window and the result is saved in the variable MyFolder.
Note: If the user clicks cancel, the dialog returns "" so you might want to write code to accomodate this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top