all groups > dotnet windows forms designtime > february 2006 >
You're in the

dotnet windows forms designtime

group:

Notify when collection changes



Notify when collection changes Greg
2/23/2006 1:54:11 PM
dotnet windows forms designtime: 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?

Re: Notify when collection changes Michael Gunter
2/24/2006 12:41:15 AM
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)

{

}

}



[quoted text, click to view]

Re: Notify when collection changes InK_
2/28/2006 12:00:00 AM
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))]

--
Regards,
Inna Stetsyak aka InK_

[quoted text, click to view]


AddThis Social Bookmark Button