Click here to Skip to main content
15,896,207 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>
	/// FileBrowserExtenderProviderBase serves as base class for file browser extenders
	/// </summary>
	[ProvideProperty("BrowseButton", typeof(Control))]
	public abstract class FileBrowserExtenderProviderBase : Component, IExtenderProvider
	{
		private Hashtable extendees;
		protected FileDialog fileDialog;

		public FileBrowserExtenderProviderBase()
		{
			extendees = new Hashtable();
		}

		// Save the contents of the FileDialog
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public System.Windows.Forms.FileDialog FileDialog
		{
			get {  return fileDialog; }
		}

		#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 (fileDialog.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 = fileDialog.FileName;
						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