I have a control derived from UserControl with a collection member. When
the designer invokes its collection editor and the developer adds/removes
items, can I get notified of that in my control? When they hit OK on the
collection editor dialog, I'd like to invoke some of my code. How do I do
that?
Michael Gunter - 24 Feb 2006 05:41 GMT
Depending on your intent, you may need to implement a custom designer. If
your collection is of classes inherited from Components, it's quite easy.
All you have to do is catch the ComponentRemoving/ComponentRemoved events
from IComponentChangeService. Here's some code I have lying around.
public class DteAddInDesigner : ComponentDocumentDesigner
{
private DteAddIn _addIn;
protected override void Dispose(bool disposing)
{
if (disposing)
{
IComponentChangeService c = (IComponentChangeService)
GetService(typeof(IComponentChangeService));
c.ComponentAdded -= new ComponentEventHandler(OnComponentAdded);
c.ComponentRemoving -= new ComponentEventHandler(OnComponentRemoving);
c.ComponentRename -= new ComponentRenameEventHandler(OnComponentRename);
}
base.Dispose(disposing);
}
public override void Initialize(System.ComponentModel.IComponent component)
{
base.Initialize(component);
// Record instance of control we're designing
_addIn = (DteAddIn) component;
// Hook up events
IComponentChangeService c = (IComponentChangeService)
GetService(typeof(IComponentChangeService));
c.ComponentAdded += new ComponentEventHandler(OnComponentAdded);
c.ComponentRemoving += new ComponentEventHandler(OnComponentRemoving);
c.ComponentRename += new ComponentRenameEventHandler(OnComponentRename);
}
public override void OnSetComponentDefaults()
{
_addIn.Text = _addIn.Site.Name;
base.OnSetComponentDefaults ();
}
private void OnComponentAdded(object sender, ComponentEventArgs e)
{
}
private void OnComponentRemoving(object sender, ComponentEventArgs e)
{
}
private void OnComponentRename(object sender, ComponentRenameEventArgs e)
{
}
}
>I have a control derived from UserControl with a collection member. When
>the designer invokes its collection editor and the developer adds/removes
>items, can I get notified of that in my control? When they hit OK on the
>collection editor dialog, I'd like to invoke some of my code. How do I do
>that?
InK_ - 28 Feb 2006 08:36 GMT
Greg
You can do it also in the following way:
1. You can impelement your collection derived from CollectionBase.
2. Impelement your CollectionEditor.
It can override e.g. CreateInstance and DestroyInstance methods.They will
give you possibility to control additions and removals.
You could store your control in your CollectionEditor so that you'd be able
to call any control methods on addition and remove.
3. Add an attribute to your custom collection
[Editor(typeof(MyCollectionEditor),
typeof(System.Drawing.Design.UITypeEditor))]

Signature
Regards,
Inna Stetsyak aka InK_
> I have a control derived from UserControl with a collection member. When
> the designer invokes its collection editor and the developer adds/removes
> items, can I get notified of that in my control? When they hit OK on the
> collection editor dialog, I'd like to invoke some of my code. How do I do
> that?