Click here to Skip to main content
15,887,027 members
Articles / Programming Languages / C#

Extender provider to simplify file/folder selection

Rate me:
Please Sign up or sign in to vote.
4.71/5 (20 votes)
5 Jan 2005CPOL4 min read 59.5K   427   44  
Common task of letting the user select a file or folder wrapped in an IExtenderProvider implementation.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;

namespace MavComponents
{
	/// <summary>
	/// Extender provider that shows a FolderBrowserDialog and automatically sets the
	/// appropriate control's text to the selected folder.
	/// </summary>
	[ToolboxBitmap(typeof(FolderBrowserDialog))]
	[ProvideProperty("BrowseButton", typeof(Control))]
	public class FolderBrowserExtenderProvider : Component, IExtenderProvider
	{
		private Hashtable extendees;
		protected FolderBrowserDialog folderDialog;

		public FolderBrowserExtenderProvider()
		{
			extendees = new Hashtable();
			folderDialog = new FolderBrowserDialog();
		}

		// Save the contents of the FolderBrowserDialog
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		[Description("The FolderBrowserDialog shown when the assigned BrowseButton is clicked")]
		public System.Windows.Forms.FolderBrowserDialog FolderBrowserDialog
		{
			get {  return folderDialog; }
		}
		
		#region IExtenderProvider Member

		public bool CanExtend(object extendee)
		{
			return (extendee is Control);
		}

		[DefaultValue(null)]
		public Control GetBrowseButton(Control extendee)
		{
			if (extendees.Contains(extendee))
				return (Control)extendees[extendee];
			else
				return null;
		}

		public void SetBrowseButton(Control extendee, Control browseButton)
		{
			if (DesignMode && (extendee == browseButton))
			{
				string correctCode = "SetBrowseButton("+extendee.Name+", "+browseButton.Name+");";
				throw new ApplicationException("Because of a bug in the code generation you cannot set a control to be its own BrowseButton in the visual designer.\r\nAdding the call to SetBrowseButton() to code does work, though.\r\n\r\nYou might consider adding this line to your code:\r\n"+correctCode);
			}

			if (extendees.Contains(extendee))
			{
				Control btn = extendees[extendee] as Control;
				btn.Click -= new EventHandler(browseButton_Click);
				extendees.Remove(extendee);
			}

			if (browseButton == null)
				return;

            extendees[extendee] = browseButton;
			browseButton.Click += new EventHandler(browseButton_Click);
		}


		#endregion

		private void browseButton_Click(object sender, EventArgs e)
		{
			Control btn = sender as Control;

			if (folderDialog.ShowDialog(btn.FindForm()) == DialogResult.OK)
			{
				// Find the accompanying target and set it's text
				foreach (DictionaryEntry ent in extendees)
				{
					if (ent.Value == btn)
					{
						Control target = ent.Key as Control;
						target.Text = folderDialog.SelectedPath;
						return;
					}
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) 4voice AG
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions