Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / June 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marina - 11 Jun 2004 15:50 GMT
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
DRaiko - 14 Jun 2004 09:16 GMT
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.

> 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
[quoted text clipped - 9 lines]
> Thanks,
> Marina
Marina - 14 Jun 2004 14:46 GMT
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

> Hi, Marina!
>
[quoted text clipped - 26 lines]
> > Thanks,
> > Marina
Marina - 14 Jun 2004 17:55 GMT
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%40tk
msftngp03&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26selm%3DusZytQPpBH
A.2172%2540tkmsftngp03


> 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?
[quoted text clipped - 43 lines]
> > > Thanks,
> > > Marina
DRaiko - 15 Jun 2004 08:51 GMT
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.

> 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?
[quoted text clipped - 43 lines]
> > > Thanks,
> > > Marina

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.