views:

35

answers:

2

Actually i wrote a UserControl which acts as a container for other (let's call it) NestedUserControls.

I implemented a programmatically way to add such NestedUserControls to my UserControl (through an App(string name) function). But what i actually more like would be some kind of List as a public property for my UserControl.

But the problem is, that my NestedUserControl needs to be put into the private UserControl.panel.Controls list. Within my own written Add() function, this is no big problem. But how can i accomplish this problem when i use the List<NestedUserControl> property? I don't get any informations about, when the list changes in any way.

The only idea i actually found would be to create some kind of EventList<> which throws an event whenever the list gets changed. So would this be the 'right' way or do you have any better approaches (or maybe there exists already some kind of EventList within .Net framework??

Oh, before i forget: I'm using VS2008, C#, WinForms, .Net 3.5 SP1

+1  A: 

If I understand your question correctly, you either have to use an observable collection for storing nested user controls, unfortunately the only one I know is in WPF assembly: http://msdn.microsoft.com/en-us/library/ms668604.aspx or, preferably, expose only IList<NestedUserControl> to the users of your usercontrol, and implement this interface in a way that it delegates most of the work to UserControl.panel.Controls. An exaple how to implement IList: http://damieng.com/blog/2006/06/14/Observing_change_events_on_a_ListT

Grzenio
At first, you understand my question correctly. ;-) I also found the observable collection and indeed it is only available in WPF. So this is not available for me. :-( Your second guess is what i also comes to my mind (with the own written EventList class). Maybe someone else has any other idea...
Oliver
A: 

So to get more clearity within this problem, here my solution:

What i'd liked to do was, adding a List to my UserControlContainer, which can be easily changed at run-time or design-time. To get it correctly to work on design-time, i had to inherit from CollectionBase instead of List<> and added the DesignerSerializationVisibility(DesignerSerializationVisibility.Content) attribut. The CollectionBase itself provides some events for Insert, Remove and Clear.

After implementing these functions and adding firing of a my own defined event ListChangedEvent to the outer world, i was able to get informations about any list change.

The only problem you'll have, is that the CollectionEditor won't delete any objects from your list. Instead it seems to work with a second internal list. After hitting ok, your list will be cleared and filled up with the other events. So don't miss to handle the CollectionBase.OnClear or CollectionBaseOnClearComplete functions!

Oliver