Click here to Skip to main content
15,894,825 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.6K   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.ComponentModel;
using System.Globalization;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Collections;
using CustomControls.ApplicationBlocks;
using CustomControls.Editors;
using System.Drawing;

namespace DynamicProperties
{
	[TypeConverter(typeof(Employee_Converter))]
	public class Employee:DynamicTypeDescriptor
	{

		private string _Name=string.Empty;
		private int _Age=-1;
	
		//Avoid using the name "Name" for your property  in  objects that implements IComoponent, because it may interfier with the  Design-Time property "Name" 
		[Category("EmployeeInfo")]
		public string FullName
		{
			get{return _Name;}
			set
			{
				if(value!=_Name)
				{
					_Name= value;
				}
			}
		}

		[Category("EmployeeInfo")]
		public int Age
		{
			get{return _Age;}
			set
			{
				if(value!=_Age)
				{
					_Age= value;
				}
			}
		}
		public Employee()
		{
			
		}
		public Employee(string Name, int Age)
		{
			this._Name= Name;
			this._Age= Age;
		}

		#region "Localization"

		public override string GetLocalizedName(string Name)
		{			
			string name=CustomControls.Globalization.Dictionary.Translate(Name);
			if(name!=null ){return name;}
			return base.GetLocalizedName (Name);
		}

		public override string GetLocalizedDescription(string Description)
		{
			string descr=CustomControls.Globalization.Dictionary.Translate(Description + "_Descr");
			if(descr!=null ){return descr;}
			return base.GetLocalizedName (Description);
		}

        
		#endregion
	}

	internal class Employee_Converter:ExpandableObjectConverter
	{
	
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) 
		{
			if (destType == typeof(InstanceDescriptor)) 
			{
				return true;
			}
			return base.CanConvertTo(context, destType);
		}	
		
		
		public override object  ConvertTo(ITypeDescriptorContext context,CultureInfo info,object value,Type destType )
		{
			if (destType == typeof(InstanceDescriptor)) 
			{
				return new InstanceDescriptor(typeof(Employee).GetConstructor(new Type[0]),null,false);
			}
			return base.ConvertTo(context,info,value,destType);
		}

		
	}
	public class Employee_Collection:CollectionBase
	{
		

		public Employee_Collection()
		{
			
		}

		public int Add(Employee e)
		{
			return this.InnerList.Add(e);
		}

		public void AddRange(Employee[] es)
		{
			this.InnerList.AddRange(es);	
		}

		public void Remove(Employee e)
		{
			InnerList.Remove(e);
		}

		public new void RemoveAt(int index)
		{
			InnerList.RemoveAt(index);
		}

		public bool Contains(Employee e)
		{
			return InnerList.Contains(e);
		}

		public Employee this[int index]
		{
			get{return (Employee)this.InnerList[index];}
			set{this.InnerList[index]= value;}
		}
	}
	public class Employees_CollectionEditorForm:CustomControls.Editors.CustomCollectionEditorForm
	{

		public Employees_CollectionEditorForm()
		{
				this.ImageList=CustomControls.BaseClasses.ImageRes.ImageList;
		}


		protected override void SetProperties(TItem titem, object reffObject)
		{
			
			if(reffObject is Employee)
			{
				Employee emp =(Employee)reffObject;
			
				titem.Text=emp.FullName;
				if(emp.Age<18)
				{
					titem.ForeColor=Color.Red;
					titem.ImageIndex=12;
					titem.SelectedImageIndex=12;
				}
				else
				{
					titem.ForeColor=Color.Black;
					titem.ImageIndex=11;
					titem.SelectedImageIndex=11;
				}
			}
			
					
		}

		
	}


	public class Employees_CollectionEditor:CustomCollectionEditor
	{
		protected override CustomCollectionEditorForm CreateForm()
		{
			return new Employees_CollectionEditorForm();
		}

	}
}

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