Click here to Skip to main content
15,886,035 members
Articles / Programming Languages / C#

DropDownPanel

Rate me:
Please Sign up or sign in to vote.
4.87/5 (26 votes)
11 Feb 2007Public Domain4 min read 120.8K   2K   107  
A template for custom ComboBoxes
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace AT.STO.UI.Win
{
	/// <summary>
	/// The form that pops up instead of the dropdown portion of the 
	/// DropDownPanel's combobox. It containes the actual control to
	/// display.
	/// </summary>
	internal partial class DropDownForm : Form, IDropDownAware
	{
	#region Private Variable Declaration
		private IDropDownAware _control = null;
	#endregion
	#region Constructor / Destructor
		/// <summary>
		/// Default Constructor
		/// </summary>
		public DropDownForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Constructor to initialize the for with the control to display.
		/// </summary>
		/// <param name="Ctrl">The control to display.</param>
		public DropDownForm(IDropDownAware Ctrl) : this()
		{	
			if (Ctrl != null)
			{
				_control = Ctrl;
				
				InitializeControl(_control as Control);
			}
		}
	#endregion
	#region Form Events
		protected override void OnClosing(CancelEventArgs e)
		{
			this.Controls.Remove(_control as Control);
			base.OnClosing(e);
		}

		protected override void OnShown(EventArgs e)
		{
			base.OnShown(e);
			
			_control.FinishEditing += new DropDownValueChangedEventHandler(Ctrl_FinishEditing);
			_control.ValueChanged += new DropDownValueChangedEventHandler(Ctrl_ValueChanged);
		}
	#endregion
	#region Event Handler
	    private void Ctrl_FinishEditing(object sender, DropDownValueChangedEventArgs e)
	    {
			if (this.FinishEditing != null)
			{
				this.FinishEditing(this, e);
			}

			_control.FinishEditing -= new DropDownValueChangedEventHandler(Ctrl_FinishEditing);
			_control.ValueChanged -= new DropDownValueChangedEventHandler(Ctrl_ValueChanged);
	    }
	    
	    private void Ctrl_ValueChanged(object sender, DropDownValueChangedEventArgs e)
	    {
			if (this.ValueChanged != null)
			{
				this.ValueChanged(this, e);
			}
	    }
	#endregion
	#region IDropDownAware Implementation
		/// <summary>
		/// Fired either on OK, Cancel or a click outside the control to indicate
		/// that the user has finished editing.
		/// </summary>
		public event DropDownValueChangedEventHandler FinishEditing;
		
		/// <summary>
		/// Fired on any change of the controls's value during the editing process. 
		/// </summary>
		public event DropDownValueChangedEventHandler ValueChanged;
		
		/// <summary>
		/// Gets or sets the controls' value.
		/// </summary>
		public object Value
		{
			get { return _control.Value; }
			set { _control.Value = value; }
		}
	#endregion
	#region Private Methods
		private void InitializeControl(Control Ctrl)
		{
			Size	size	= Ctrl.Size;
			Size	inner	= this.ClientRectangle.Size;
			Size	outer	= this.Size;
			int		gap		= outer.Width - inner.Width;
			
			size.Width += gap;
			size.Height += gap;
			
			this.Size = size;
			this.Controls.Add(Ctrl);
			Ctrl.Location = new Point(0, 0);
			Ctrl.Visible = true;
			Ctrl.Invalidate();
		}
	#endregion
	}
}

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 A Public Domain dedication


Written By
Technical Lead Artaker Computersysteme GmbH
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions