Click here to Skip to main content
12,695,624 members (25,520 online)
Click here to Skip to main content
Articles » Languages » C# » Windows Forms » Downloads

Stats

102K views
4.3K downloads
87 bookmarked
Posted

Unleash PropertyGrid with Dynamic Properties and Globalization

, 29 Jan 2004 CPOL
The article presents a way to enhance the use of the PropertyGid control with dynamic properties and globalization
using System;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel.Design;

namespace CustomControls.Editors
{
	public class CustomCollectionEditor: System.Drawing.Design.UITypeEditor 
	{

		public delegate void CollectionChangedEventHandler(object sender, object instance, object value);
		public event CollectionChangedEventHandler CollectionChanged;

		private ITypeDescriptorContext _context;
		
		private IWindowsFormsEditorService edSvc = null;

		public CustomCollectionEditor()
		{
		
			
		}

		
		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) 
		{
			if (context != null	&& context.Instance != null	&& provider != null) 
			{
				object originalValue=value;
				_context=context;
				edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

				if (edSvc != null) 
				{
					CustomCollectionEditorForm collEditorFrm = CreateForm();
					collEditorFrm.ItemAdded+= new CustomCollectionEditorForm.InstanceEventHandler(ItemAdded);
					collEditorFrm.ItemRemoved+= new  CustomCollectionEditorForm.InstanceEventHandler(ItemRemoved);
				
					collEditorFrm.Collection=(System.Collections.CollectionBase)value;
					
					
					context.OnComponentChanging();
					if(edSvc.ShowDialog(collEditorFrm)==DialogResult.OK)
					{
						OnCollectionChanged(context.Instance, value);
						context.OnComponentChanged();
					}					

				
					
				}
			}

			return value;
		}

		
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
		{
			if (context != null && context.Instance != null) 
			{
				return UITypeEditorEditStyle.Modal;
			}
			return base.GetEditStyle(context);
		}

		
		private void ItemAdded(object sender, object item)
		{
			
			if(_context!=null && _context.Container!=null)
			{
				IComponent icomp=item as IComponent;
				if(icomp !=null )
				{	
					_context.Container.Add(icomp);									
				}
			}

		}

	
		private void ItemRemoved(object sender, object item)
		{			
			if(_context!=null && _context.Container!=null)
			{
				IComponent icomp=item as IComponent;
				if(icomp!=null)
				{						
					_context.Container.Remove(icomp);
				}
			}

		}

	
		protected virtual void OnCollectionChanged(object instance ,object value)
		{
			if(CollectionChanged !=null)
			{
				CollectionChanged(this, instance,value);
			}
		}
		

		protected virtual CustomCollectionEditorForm CreateForm()
		{
			return new CustomCollectionEditorForm();
		}

		
	}
}

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)

Share

About the Author

Daniel Zaharia
Web Developer
Romania Romania
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 30 Jan 2004
Article Copyright 2004 by Daniel Zaharia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid