Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

Unleash PropertyGrid with Dynamic Properties and Globalization

Rate me:
Please Sign up or sign in to vote.
4.97/5 (24 votes)
29 Jan 2004CPOL9 min read 126.1K   4.8K   88  
The article presents a way to enhance the use of the PropertyGid control with dynamic properties and globalization
using System;
using System.Windows.Forms;
using System.ComponentModel;
using CustomControls.BaseClasses;
using CustomControls.HelperClasses;

namespace CustomControls.Win32Controls
{
	public class DropDownListBox:DropDownListBase
	{

		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;
			}
		}

		[Browsable(false)]
		[DefaultValue(typeof(Int32),"-1")]
		public int SelectedIndex
		{
			get{return List.SelectedIndex;}
			set
			{
				if(value!=List.SelectedIndex)
				{
					List.SelectedIndex=value;
					OnItemSelected(new ItemSelectedEventArgs(List.SelectedItem, List.SelectedIndex));
				}
			}
		}

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

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

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

		public DropDownListBox():base()
		{
			List.BorderStyle=System.Windows.Forms.BorderStyle.None;
			List.MouseUp+= new MouseEventHandler(List_MouseUp);

			
		}

		protected override void OnDropDown(System.EventArgs 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)
		{
			DroppedDown=false;
			OnItemSelected(new ItemSelectedEventArgs(List.SelectedItem, List.SelectedIndex));
		}

		

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

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


		

		protected virtual void OnItemSelected(ItemSelectedEventArgs e)
		{

			Text=List.Text;
			editControl.SelectAll();
			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