Click here to Skip to main content
15,884,298 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.Windows.Forms;

using AT.STO.UI.Win;

namespace Demo
{
	internal class DropDownTree : TreeView, IDropDownAware
	{
	#region DropDownTree
		public DropDownTree()
		{
		}
	#endregion
	#region TreeView Events
		/// <summary>
		/// Allow keeping track of the editing process.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnAfterSelect(TreeViewEventArgs e)
		{
			base.OnAfterSelect(e);
			
			if (ValueChanged != null)
			{
				ValueChanged(this, new DropDownValueChangedEventArgs(e.Node));
			}
		}
		
		/// <summary>
		/// A double click on a node counts as finish editing.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnDoubleClick(EventArgs e)
		{
			base.OnDoubleClick(e);
			
			TreeNode node = HitTest(PointToClient(Cursor.Position)).Node;
			
			if ((FinishEditing != null) && (node != null))
			{
				FinishEditing(this, new DropDownValueChangedEventArgs(node));
			}
		}

		/// <summary>
		/// ENNTER counts as finish editing, ESC as cancel (null is returned).
		/// </summary>
		/// <param name="e"></param>
		protected override void OnKeyUp(KeyEventArgs e)
		{
			base.OnKeyUp(e);
	
			if (FinishEditing != null)
			{			
				switch (e.KeyCode)
				{
					case Keys.Enter:
						FinishEditing(this, new DropDownValueChangedEventArgs(Value));
						break;
					case Keys.Escape:
						FinishEditing(this, new DropDownValueChangedEventArgs(null));
						break;
				}
			}
		}
	#endregion
	#region IDropDownAware Implementation
		public event DropDownValueChangedEventHandler FinishEditing;
		public event DropDownValueChangedEventHandler ValueChanged;

		public object Value
		{
			get { return base.SelectedNode; }
			set 
			{ 
				if (value is DropDownNode)
				{
					base.SelectedNode = value as DropDownNode; 
				}
			}
		}

		#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