|
-
Oct 28th, 2009, 02:09 PM
#1
Adding ToolStrips to a container during design-time (via collection editor)
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:
Public Class cToolStripPanel
Inherits ToolStripPanel
Private _ToolStrips As New ToolStripCollection(Me)
Public ReadOnly Property ToolStrips() As ToolStripCollection
Get
Return _ToolStrips
End Get
End Property
End Class
Public Class ToolStripCollection
Inherits System.Collections.ObjectModel.Collection(Of ToolStrip)
Private _panel As cToolStripPanel
Public Sub New(ByVal panel As cToolStripPanel)
_panel = panel
End Sub
Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As System.Windows.Forms.ToolStrip)
MyBase.InsertItem(index, item)
_panel.Controls.Add(item)
End Sub
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!
-
Oct 28th, 2009, 02:12 PM
#2
Re: Adding ToolStrips to a container during design-time (via collection editor)
By the way, if I use the following code instead, which adds Buttons to a Panel instead of ToolStrips to a ToolStripPanel, then it works just fine!
vb.net Code:
Public Class ButtonPanel Inherits Panel Private _Buttons As New ButtonCollection(Me) Public ReadOnly Property Buttons() As ButtonCollection Get Return _Buttons End Get End Property End Class Public Class ButtonCollection Inherits System.Collections.ObjectModel.Collection(Of Button) Private _panel As ButtonPanel Public Sub New(ByVal panel As ButtonPanel) _panel = panel End Sub Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As System.Windows.Forms.Button) MyBase.InsertItem(index, item) _panel.Controls.Add(item) End Sub End Class
Now I can use the Buttons property of this panel to add/remove them via a collection editor window (very similar to how you can add/remove TabPages to a TabControl).
This code is exactly the same as above... The only difference is that I replaced the ToolStripPanel with Panel* and ToolStrip with Button. I can't figure out why this works with buttons but not with ToolStrips.
*The problem is not caused by using a ToolStripPanel instead of a Panel. I cannot add ToolStrips to a normal Panel this way either.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|