123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 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.CollectionEditor
- {
- 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();
- }
-
- }
- }
|