Click here to Skip to main content
15,891,621 members
Articles / Programming Languages / C#

How to Edit and Persist Collections with CollectionEditor

Rate me:
Please Sign up or sign in to vote.
4.99/5 (97 votes)
26 Nov 2003CPOL16 min read 416.2K   13.6K   227  
The article demonstrates how to edit and persist collections with CollectionEditor.
using System;
using System.Windows.Forms;
using System.ComponentModel;
using CustomControls.BaseClasses;
using CustomControls.HelperClasses;


using System.Drawing.Design;

namespace CustomControls.Win32Controls
{
	public class DropDownListBoxButton:DropDownButtonBase 
	{
		public delegate void ItemSelectedEventHandler(object sender, ItemSelectedEventArgs e);
		[Category("Action")]
		public event ItemSelectedEventHandler ItemSelected;

		private ListBox _List= new ListBox();
		private int _MaxDropDawnItems=8;
		private int _MinListWidth=140;



		protected override System.Windows.Forms.Control HostedControl
		{
			get
			{
				return _List;
			}
		}

		[Category("Behavior")]
		[DefaultValue(typeof(System.Int32),"140")]
		public int MinListWidth
		{
			get{return _MinListWidth;}
			set{_MinListWidth= value;}
		}



		[Category("Behavior")]
		[DefaultValue(typeof(System.Int32),"8")]
		public int MaxDropDownItems
		{
			get{return _MaxDropDawnItems;}
			set{_MaxDropDawnItems=value;}
		}


		[Browsable(false)]
		public ListBox List
		{
			get{return _List;}
		}

		public DropDownListBoxButton()
		{
			_List.BorderStyle=System.Windows.Forms.BorderStyle.None;
			_List.MouseUp+= new MouseEventHandler(List_MouseUp);
		}



		protected override void OnDropDown(EventArgs e)
		{
			base.OnDropDown(e);

			if (Text!=string.Empty)	{_List.SelectedIndex=_List.FindString(Text);}
			else{_List.SelectedIndex=-1;}

			int lHeight,lWidth;
			lHeight=Math.Max(List.ItemHeight,Math.Min(MaxDropDownItems, List.Items.Count)*List.ItemHeight);
			lWidth=Math.Max(this.Width+20,MinListWidth);
			dropDownForm.Size= new System.Drawing.Size(lWidth,lHeight+4);
			
		}


				
		private void List_MouseUp(object sender , MouseEventArgs e)
		{
			State=Enumerations.ButtonState.Normal;
			OnItemSelected(new ItemSelectedEventArgs(List.SelectedItem,List.SelectedIndex));
		}

	
		

		protected override void OnNextItem()
		{
			if(_List.SelectedIndex<_List.Items.Count-1)
			{
				_List.SelectedIndex+=1;
			}
		}

		protected override void OnPrevItem()
		{
			if(_List.SelectedIndex>0)
			{
				_List.SelectedIndex-=1;
			}
		}

	


		protected override void OnSelectItem(bool DefaultValue)
		{
			if(DefaultValue)
			{
				if(List.Items.Count>0)
				{
					OnItemSelected(new ItemSelectedEventArgs(List.Items[0],0));
				}
				else
				{
					OnItemSelected(new ItemSelectedEventArgs(null,-1));
				}
			}
			else
			{
				OnItemSelected(new ItemSelectedEventArgs(_List.SelectedItem,_List.SelectedIndex));
			}
		}



		protected virtual void OnItemSelected(ItemSelectedEventArgs e)
		{
			if (ItemSelected!=null)
			{
				ItemSelected(this,e);
			}
		}

		

	}
}

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
Web Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions