Results 1 to 2 of 2

Thread: Adding ToolStrips to a container during design-time (via collection editor)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    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!

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Public Class ButtonPanel
    2.     Inherits Panel
    3.  
    4.     Private _Buttons As New ButtonCollection(Me)
    5.     Public ReadOnly Property Buttons() As ButtonCollection
    6.         Get
    7.             Return _Buttons
    8.         End Get
    9.     End Property
    10.  
    11. End Class
    12.  
    13. Public Class ButtonCollection
    14.     Inherits System.Collections.ObjectModel.Collection(Of Button)
    15.  
    16.     Private _panel As ButtonPanel
    17.     Public Sub New(ByVal panel As ButtonPanel)
    18.         _panel = panel
    19.     End Sub
    20.  
    21.     Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As System.Windows.Forms.Button)
    22.         MyBase.InsertItem(index, item)
    23.         _panel.Controls.Add(item)
    24.     End Sub
    25.  
    26. 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
  •  



Click Here to Expand Forum to Full Width