all groups > dotnet windows forms designtime > june 2004 >
You're in the

dotnet windows forms designtime

group:

Deleting components that are part of a collection when parent is deleted


Deleting components that are part of a collection when parent is deleted Marina
6/11/2004 10:50:05 AM
dotnet windows forms designtime:
Let's say there is a component (component A) with a collection for a
property. This collection is a collection of components (component B) with
DesignTimeVisible = False, so they don't show up in the component tray.

When adding to the collection, there is a variable declared for each
component B, which is instantiated, etc.

When A is deleted - all the declarations and instantiations of B stay.

How to get all the B's deleted, when the A they belong to is deleted?

Thanks,
Marina

Re: Deleting components that are part of a collection when parent is deleted draiko NO[at]SPAM rhenus.de
6/14/2004 1:16:32 AM
Hi, Marina!

Look at ComponentRemoving() / ComponentRemoved() of IComponentChangeService.
Hook the event (Removed is better, if you dont need to aks the user
and prevent removing) in the .Site prop of the component A.
You have all you need -- (i) the name of the component going to be deleted
so the component can check whether exactly it is killed;
and (ii) all pointers to the components (Bs) to remove.

You can get IComponentChangeService in .Site calling the GetService()
with a propper param. Dont forget to unhook the event when you are ready.
Otherwise the component A will not be actually killed and thus
the B components also. Consequences can be exciting. But hardly desired.

HTH,
Dima.

[quoted text, click to view]
Re: Deleting components that are part of a collection when parent is deleted Marina
6/14/2004 9:46:56 AM
Implementing this interface means defining tons of events that don't really
have anything to do with the component - and then writing an event handler?

I looked at classes like the DataSet - which creates DataTable's in the
designer. It does not implement this interface.

I am still at a loss at how to actually have the code for these components
removed from the designer.

Is there a complete example I can look at?

Thanks


[quoted text, click to view]

Re: Deleting components that are part of a collection when parent is deleted Marina
6/14/2004 12:55:20 PM
Found an example, here is the thread in case anyone is curious:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=usZytQPpBHA.2172%40tkmsftngp03&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26selm%3DusZytQPpBHA.2172%2540tkmsftngp03

[quoted text, click to view]

Re: Deleting components that are part of a collection when parent is deleted draiko NO[at]SPAM rhenus.de
6/15/2004 12:51:41 AM
Halt! Halt!

Marina! You need not implement interfaces. Everything is already there.
Ten lines are enough. (well, 20).

(i) Override the Site Property of component A:

public override ISite Site{
get{ return base.Site; }
set{
// get change service
IComponentChangeService chServ = (IComponentChangeService)base.GetService(
typeof( IComponentChangeService));
if( chServ != null){
// may be not the 1st setter call, so unhook:
chServ.ComponentRemoved -= new ComponentEventHandler( chServ_Removed);
}

base.Site = value;

// get anew (the Site is different)
chServ = (IComponentChangeService)base.GetService(
typeof( IComponentChangeService));
// hook
if( chServ != null){
chServ.ComponentRemoved += new ComponentEventHandler( chServ_Removed);
}
}
}

(ii) handle removing:
private void chServ_Removed( object sender, ComponentEventArgs e){
IDesignerHost desHost = (IDesignerHost)sender;
if( desHost.Loading)
return; // all components are removed when designer unloads.
// Ignore in this case. ---------->>>>>>>>>>>>>

if( object.ReferenceEquals( e.Component, this)){
// yes, they want to kill me! Imust kill all my children.
foreach( B b in b_collection){
desHost.DestroyComponent( b);
}

// unhook
IComponentChangeService chServ = (IComponentChangeService)base.GetService(
typeof( IComponentChangeService));
if( chServ != null){
chServ.ComponentRemoved -= new ComponentEventHandler( chServ_Removed);
}
}
}

(iii) be happy.

Check for typos, but the idea must be clear.

It would be better to enclose removing all b-components in one transaction.
(mainly to be able to undo removing in one click).

If i understand what you need.

HTH,
Dima.

[quoted text, click to view]
AddThis Social Bookmark Button