CustomCollectionEditor.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Windows.Forms.Design;
  3. using System.ComponentModel;
  4. using System.Drawing.Design;
  5. using System.Windows.Forms;
  6. using System.Collections;
  7. using System.ComponentModel.Design;
  8. namespace CustomControls.CollectionEditor
  9. {
  10. public class CustomCollectionEditor: System.Drawing.Design.UITypeEditor
  11. {
  12. public delegate void CollectionChangedEventHandler(object sender, object instance, object value);
  13. public event CollectionChangedEventHandler CollectionChanged;
  14. private ITypeDescriptorContext _context;
  15. private IWindowsFormsEditorService edSvc = null;
  16. public CustomCollectionEditor()
  17. {}
  18. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  19. {
  20. if (context != null && context.Instance != null && provider != null)
  21. {
  22. object originalValue=value;
  23. _context=context;
  24. edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  25. if (edSvc != null)
  26. {
  27. CustomCollectionEditorForm collEditorFrm = CreateForm();
  28. collEditorFrm.ItemAdded+= new CustomCollectionEditorForm.InstanceEventHandler(ItemAdded);
  29. collEditorFrm.ItemRemoved+= new CustomCollectionEditorForm.InstanceEventHandler(ItemRemoved);
  30. collEditorFrm.Collection=(System.Collections.CollectionBase)value;
  31. context.OnComponentChanging();
  32. if(edSvc.ShowDialog(collEditorFrm)==DialogResult.OK)
  33. {
  34. OnCollectionChanged(context.Instance, value);
  35. context.OnComponentChanged();
  36. }
  37. }
  38. }
  39. return value;
  40. }
  41. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  42. {
  43. if (context != null && context.Instance != null)
  44. {
  45. return UITypeEditorEditStyle.Modal;
  46. }
  47. return base.GetEditStyle(context);
  48. }
  49. private void ItemAdded(object sender, object item)
  50. {
  51. if(_context!=null && _context.Container!=null)
  52. {
  53. IComponent icomp=item as IComponent;
  54. if(icomp !=null )
  55. {
  56. _context.Container.Add(icomp);
  57. }
  58. }
  59. }
  60. private void ItemRemoved(object sender, object item)
  61. {
  62. if(_context!=null && _context.Container!=null)
  63. {
  64. IComponent icomp=item as IComponent;
  65. if(icomp!=null)
  66. {
  67. _context.Container.Remove(icomp);
  68. }
  69. }
  70. }
  71. protected virtual void OnCollectionChanged(object instance ,object value)
  72. {
  73. if(CollectionChanged !=null)
  74. {
  75. CollectionChanged(this, instance,value);
  76. }
  77. }
  78. protected virtual CustomCollectionEditorForm CreateForm()
  79. {
  80. return new CustomCollectionEditorForm();
  81. }
  82. }
  83. }