Hey all,

I am trying to extend the functionality of the ToolStripPanel control (which is a panel hosting ToolStrips that can be moved during design-time, as I'm sure you know).

One thing I wanted to add was a 'ToolStrips' property, which would be a collection of ToolStrips the user can edit via the collection editor in the designer. So instead of manually placing ToolStrips on the ToolStripPanel via the toolbox, the user can just use the property to add/remove (or even edit) them. This is of course similar to how you add/remove/edit TabPages in a TabControl.

So, I came up with this code, which I have used successfully in the past for other controls:
vb.net Code:
  1. Public Class cToolStripPanel
  2.     Inherits ToolStripPanel
  3.  
  4.     Private _ToolStrips As New ToolStripCollection(Me)
  5.     Public ReadOnly Property ToolStrips() As ToolStripCollection
  6.         Get
  7.             Return _ToolStrips
  8.         End Get
  9.     End Property
  10.  
  11. End Class
  12.  
  13. Public Class ToolStripCollection
  14.     Inherits System.Collections.ObjectModel.Collection(Of ToolStrip)
  15.  
  16.     Private _panel As cToolStripPanel
  17.     Public Sub New(ByVal panel As cToolStripPanel)
  18.         _panel = panel
  19.     End Sub
  20.  
  21.     Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As System.Windows.Forms.ToolStrip)
  22.         MyBase.InsertItem(index, item)
  23.         _panel.Controls.Add(item)
  24.     End Sub
  25.  
  26. End Class

The cToolStripPanel control inherits the ToolStripPanel control and adds the ToolStrips property, which is of type ToolStripCollection. That class, in turn, inherits the Collection(Of ToolStrip). In the InsertItem method, I add the 'item' ToolStrip to the cToolStripPanel. Of course I also need to override the RemoveItem / ClearItems methods but that's for another time.

I am 100% sure that this code should works, at least for other controls. If you replace ToolStrip with Button (for example), I can add buttons via the property during design-time just fine, exactly the way I want it.

But it seems that ToolStrips are special. When I try to Add a toolstrip (via the collection editor Add button), a null reference exception occurs. I've been trying to debug this all day and I've finally come up with a possible candidate for the problem... (I've actually used design-time debugging for the first time ever, which was pretty cool, and which showed me that the InsertItem method is not run!)

I think the problem is that the collection editor cannot create a new instance of the ToolStrip class. I can remember when trying to inherit from the ToolStrip, I was forced to add a constructor, because the ToolStrip "has more than one constructor that can be called with no arguments".
Perhaps this is causing my problem?

So on to my question... Do I (and if yes, how?) have to somehow control the creation of a new instance of the ToolStrip? Or am I completely off track and that is not what is causing the problem?

I am really stumped, and I'm sure that it's a problem with the ToolStrip class, since I can use the exact same method with other controls just fine. It's only when I change the type to ToolStrip that it starts getting angry at me....

Thanks for any pointers!