This project is read-only.

ToolStripPanelAdapter ToolStrip Order - Merging

Sep 2, 2009 at 11:03 PM


I am using a custom ToolStripPanelUIAdapterFactory to expose the top ToolStripPanel in a ToolStripContainer as a UIExtensionSite. 
My adapter is working in that I can create the UIExtensionSite and dynamically add toolStrips to it.  However I'm having an issue in assigning Toolstrip Order.

No matter what I do it seems like when I programatically add a ToolStrip to the ToolSripContainer it stacks them vertically.
Here is my adapter and adapter factory.   Any help is appreciated!

Public Class ToolStripPanelUIAdapter
    Inherits Microsoft.Practices.CompositeUI.UIElements.UIElementAdapter(Of ToolStrip)

    Private _toolStripPanel As ToolStripPanel
    Public Sub New(ByVal toolStripPanel As ToolStripPanel)
        If Not toolStripPanel Is Nothing Then
            Me._toolStripPanel = toolStripPanel
        End If
    End Sub

    Private Shared NumberOfAddedControls As Integer = 0

    Protected Overloads Overrides Function Add(ByVal uiElement As System.Windows.Forms.ToolStrip) _
        As System.Windows.Forms.ToolStrip
        If _toolStripPanel Is Nothing Then
            Throw New InvalidOperationException()
        End If

        ' remove all previously existing toolstrips
        Dim ctrls As New List(Of ToolStrip)
        For Each ctrl As ToolStrip In Me._toolStripPanel.Controls

        ' clear the toolstrips

        ' add the new toolstrip so it shows up at the end
        Me._toolStripPanel.Join(uiElement, 0)

        're-add all previous tooltsrips
        For index As Integer = ctrls.Count - 1 To 0 Step -1
            Me._toolStripPanel.Join(ctrls(index), 0)
        Return uiElement
    End Function

    Protected Overloads Overrides Sub Remove(ByVal uiElement As System.Windows.Forms.ToolStrip)
        If _toolStripPanel Is Nothing Then
            Throw New InvalidOperationException()
        End If
    End Sub
End Class

Public Class ToolStripPanelUIAdapterFactory
    Implements Microsoft.Practices.CompositeUI.UIElements.IUIElementAdapterFactory

    Public Function GetAdapter(ByVal uiElement As Object) As Microsoft.Practices.CompositeUI.UIElements.IUIElementAdapter _
        Implements Microsoft.Practices.CompositeUI.UIElements.IUIElementAdapterFactory.GetAdapter
        If TypeOf uiElement Is ToolStripPanel Then
            Return New ToolStripPanelUIAdapter(DirectCast(uiElement, ToolStripPanel))
        End If
        Throw New ArgumentException("uiElement")
    End Function

    Public Function Supports(ByVal uiElement As Object) As Boolean _
        Implements Microsoft.Practices.CompositeUI.UIElements.IUIElementAdapterFactory.Supports
        Return TypeOf uiElement Is ToolStripPanel
    End Function
End Class