Collection trouble

Owlguru
Owlguru used Ask the Experts™
on
I have created a custom control, CommandBar, essentially a wrapper for ToolBarWindow32 (the Win32 toolbar control). It has a collection of CommandBarItem objects. Each CommandBarItem is derived from Component. CommandBarItemCollection is derived from CollectionBase. I have also tried ICollection/IList/IEnumerable. Both CommandBar and CommandBarItem have item collections. The item collections in CommandBar and CommandBarItem are marked as DesignerSerializationVisibility.Content. I can add items successfully to the item collection of the CommandBar. I can also add items successfully to the collections of the items in the CommandBar item collection. But when I go to the collections of items of the collections of items of the items in the CommandBar collection and I add a new CommandBarItem with the Collection Editor, I am greeted with "Object reference not set to an instance of an object" when I click OK. Thus, I am forced to click Cancel (the message keeps appearing everytime I click OK), and the items I just added are lost. How can I get the items to persist on infinite depth levels? Do I need an object referring back to the parent collection, like Owner? How did MS do it with the TreeView and Menu controls? I am stuck and I need your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
can i see a code sample and i may be able to help.
m3
Commented:
This problem reproduces very easily -- it looks like all you need is a component which has a collection of components of its own type (either a strongly-typed Collection or a simple array). This problem does not appear to reproduce if you nest multiple collection editors of different types. Interestingly, if you have a repeating nest of collection editors (i.e., class A contains a collection of class B, which contains a collection of class A), the exception isn't thrown until you click "OK" on an editor of the same type as the last one you went to. That is, if you drill into the editors a bit and stop on a class B editor, then click OK, the exception won't be thrown until you click OK on the class A editor, then *again* on the class B editor. I'll report this bug to the VS.NET design team and we'll see what happens.

Meanwhile, I can only see two options open for you -- implement your own collection editor and use that, or, since your sub-objects inherit from Component, don't use the nested collection editors -- click OK for the current editor, then edit the one you just created. Neither solution is very good, but that's about the best I can see until this bug in the stock collection editors is fixed.

Commented:
Well, since the administrative updates brought this question back to my attention, I may as well point out that the issue appears to be fixed in the Whidbey beta.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial