Important alert: (current site time 7/15/2013 9:20:53 AM EDT)
 

article

Extending The BrowseForFolder Class

Email
Submitted on: 5/15/2002 12:54:38 AM
By: bleh 
Level: Beginner
User Rating: By 10 Users
Compatibility: C#
Views: 29438
 
     Simply adding a few more options to the BrowseForFolder class submitted by Chris Anderson.


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
Extending The Browse For Folder Dialog Class

This code is simpily extending the Browse For Folder Dialog class that was on posted on PSC by Chris Andersen. After using his code, I was interested in knowing if there was any additional options available aside from just being able to set the title. I consulted the MSDN and this is what I came up with. The above screen shot is showing the Browse For Folder dialog with StartLocation set to "MyDocuments" and Style set to "BrowseForEverything"

.StartLocation


The directory in which the browse dialog will start at. (duh) I haven't found a way to specifiy a certain path, like C:\PSC or something of that nature, but there are a number of members that we can use. They are:
  • Desktop
  • Favorites
  • MyComputer
  • MyDocuments
  • MyPictures
  • NetAndDialUpConnections
  • NetworkNeighborhood
  • Printers
  • Recent
  • SendTo
  • StartMenu
  • Templates

.Style


They style of the browse dialog. We have a few different options to choose from.
  • BrowseForComputer
  • BrowseForEverything
  • BrowseForPrinter
  • RestrictToDomain
  • RestrictToFilesystem
  • RestrictToSubfolders
  • ShowTextBox

The Code


Here's an example of how to use the above methods in the Browse For Folder dialog.

using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class BrowseForFolder : FolderNameEditor 
{ 
	FolderNameEditor.FolderBrowser bDialog;
 	
	public BrowseForFolder()
	{ 
		bDialog = new FolderNameEditor.FolderBrowser(); 
	}
 	
	public string browseDialog(string sTitle)
	{
		bDialog.Description = sTitle;
		bDialog.StartLocation = FolderNameEditor.FolderBrowserFolder.MyComputer;
		bDialog.Style = FolderNameEditor.FolderBrowserStyles.RestrictToDomain;
		bDialog.ShowDialog();
		return bDialog.DirectoryPath;
	}
	~BrowseForFolder()
	{
		bDialog.Dispose(); 
	}
}

Useage:


To use this class, make sure you add a reference to System.Design.DLL. Call the class like so:

BrowseForFolder myDialog = new BrowseForFolder();
MessageBox.Show(myDialog.browseDialog("Dialog Title Goes Here");
It's pretty simple. Thanks again to Chris Anderson for the original code.


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/15/2002 10:07:35 AMChris Andersen

Hey cool. I did something useful for a change. :p
(If this comment was disrespectful, please report it.)

 
5/16/2002 4:14:21 AMD. de Haas

Very usefull...:-)
5 from me to you both.

(If this comment was disrespectful, please report it.)

 
7/26/2002 3:17:03 AMEirik

Very usefull code, but it has one minor problem. It has no ability if the user pressed [OK] or [Cancel].
Other than that it's very clean and usefull.
(If this comment was disrespectful, please report it.)

 
11/8/2002 10:01:18 PM

When I tried this it did not show the tree that shows the folders. It did show the buttons and the text box though. Any suggestions?
(If this comment was disrespectful, please report it.)

 
11/18/2002 9:07:56 PM

I found that the code works great, it did exactly what I needed it to. It then wouldn't display the folders box anymore. I played around with it and found that icons on the form were causing the problem. Either using an imagelist or image property of a control. Haven't figured out how to get around that though.
(If this comment was disrespectful, please report it.)

 
12/10/2002 6:09:50 PM

I was trying to use your code example for browsing for computers. It seems I had to modify the FolderNameEditor.privateOptions to "BrowseForComputer" before it would give me the correct dialog. As well, I could not get it to return me the name of the computer just selected. "DirectoryPath" is always empty.

Also MSDN says not to invoke FolderNameEditor class directly in your code. Don't quite understand that warning since you obviously used it.
(If this comment was disrespectful, please report it.)

 
2/24/2003 3:14:17 PMnfs

Nice coding.

Vote of Excellent from me.

Hi,
Do you want to make money with your programming skills ?

Software Objects provide following services :

1)Sell your software.
2)Post a software to be done.
3)Bid on the software projects.
4)Buy software

Thanks and have a nice day.

Software Objects
http://www.thesoftwareobjects.com

(If this comment was disrespectful, please report it.)

 
3/12/2003 7:25:03 AM

Hi,
You didn't have to look at MSDN for this, the intellisense shows all properties that you can use. Anyway, just a remark, the showdialog method returns a dialogresult value so you can determine if the user pressed OK or cancel.
Use dlgResult = bDialog.Showdialog()
I also found that you can only create a new class and inherit from the FolderNameEditor class. You can't use this directly from within a form. (correct me if i'm wrong, please)
Greetings,
(If this comment was disrespectful, please report it.)

 
3/24/2003 10:49:56 AM

Browse for computer did not return my computer name. please help
(If this comment was disrespectful, please report it.)

 
3/24/2003 10:51:07 AM

browse for computer does not return the computer name
(If this comment was disrespectful, please report it.)

 
7/13/2004 10:46:32 AM

After some digging, I found why some people reported that with 'browse for computer' the computername wasn't returned: SHBrowseForFolder apparently never returns it in the 'path' buffer.
The computername is returned in the 'displayname' buffer only.
The class just frees that buffer after the call; by adding a _displayName member variable and corresponding property (similar to _directoryPath but without the access check), it works.

(If this comment was disrespectful, please report it.)

 
7/13/2004 10:51:49 AM

Sorry for the above response: it's partially to the wrong article/page/site.
The only thing relevant here is that browse for computer returns a result in the Displayname buffer only, and an empty string in the FolderPath buffer.

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.