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!

Folder dialog 4

Status
Not open for further replies.

robitherobber

Programmer
Feb 11, 2003
4
NL
Is it possible to get a folder only type of dialog window in vb.NET, I mean no files only folders can be seen and path to be obtained.
 
I do not know.

Maybe try to stick "*." in the place where you limit file types. In DOS this would only give you directories (and files without extensions).


Kris
 
Check out the System.Windows.Forms.Design.FolderNameEditor class
and FolderBrowserFolder, FolderBrowser objects
 
None of the method helped. Using "*." as filter generates runtime error.

Class System.Windows.Forms.Design.FolderNameEditor does not exists.

Any other suggestions for opening a folder browser?

 
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