Click here to Skip to main content
Licence Ms-PL
First Posted 25 Jun 2006
Views 37,464
Bookmarked 25 times

ToolStripPanel UIAdapter

By | 25 Jun 2006 | Article
Adding ToolStripPanel support to the Composite UI Application Block.

Introduction

I was very suppressed to find that CAB (Composite Application Block) did not support a higher level of the ToolStrip in its basic implementation. I resolved to solve this, and I added an adapter and factory for the ToolStripPanel initially, but I have been having trouble with the ToolStripContainer. So, as my first post, here is the ToolStripPanel code.

There are two parts to this. And two ways to do it. The first is to edit the existing ToolStrip UI factory to include the new code. But I do not like to do this as I may update CAB in the future and do not want to rely on having to implement the adaptations again.

The second is to inherit from the current classes in your own assembly and to adapt the class to support both. That way, you are extending, not replacing.

Here is the code for the new ToolStripUIAdapterFactory factory:

Public Class ToolStripPanelUIAdapter : _
             Inherits UIElementAdapter(Of ToolStrip)
        Private innerToolStripPanel As ToolStripPanel
        Public Sub New(ByVal objToolStripPanel As ToolStripPanel)
            Guard.ArgumentNotNull(objToolStripPanel, "objToolStripPanel")
            Me.innerToolStripPanel = objToolStripPanel
        End Sub
        Protected Overrides Function Add(ByVal uiElement As ToolStrip) _
                                                         As ToolStrip
            If Me.innerToolStripPanel Is Nothing Then
                Throw New InvalidOperationException()
            End If
            Me.innerToolStripPanel.Join(uiElement, 3)
            Return uiElement
        End Function
        Protected Overrides Sub Remove(ByVal uiElement As ToolStrip)
            If Me.innerToolStripPanel.Controls.Contains(uiElement) Then
                Me.innerToolStripPanel.Controls.Remove(uiElement)
            End If
        End Sub
End Class

As you can see, it inherits from the CAB class of the same name and overrides the same methods that it does, and adds the support for the new Adapter for the ToolStripPanel.

You can now add the factory to the UI Element Adapter Factory Catalog in the AfterShellCreated part of the main shell application:

Dim catalog As IUIElementAdapterFactoryCatalog = _
    RootWorkItem.Services.Get(Of IUIElementAdapterFactoryCatalog)()
catalog.RegisterFactory(New UIElements.ToolStripUIAdapterFactory())

The CAB framework will now handle ToolStripPanels as UI Extension Sites with the appropriate factory. Now all we need is the adapter...

Here is the code for the ToolStripPanel Adapter:

Public Class ToolStripPanelUIAdapter : Inherits UIElementAdapter(Of ToolStrip)
    Private innerToolStripPanel As ToolStripPanel
    Public Sub New(ByVal objToolStripPanel As ToolStripPanel)
        Guard.ArgumentNotNull(objToolStripPanel, "objToolStripPanel")
        Me.innerToolStripPanel = objToolStripPanel
    End Sub
    Protected Overrides Function Add(ByVal uiElement As ToolStrip) _
                                                     As ToolStrip
        If Me.innerToolStripPanel Is Nothing Then
            Throw New InvalidOperationException()
        End If
        Me.innerToolStripPanel.Join(uiElement, 3)
        Return uiElement
    End Function
    Protected Overrides Sub Remove(ByVal uiElement As ToolStrip)
        If Me.innerToolStripPanel.Controls.Contains(uiElement) Then
            Me.innerToolStripPanel.Controls.Remove(uiElement)
        End If
    End Sub
End Class

This provides the functionality to allow you to register a site that is of type ToolStripPanel:

RootWorkItem.UIExtensionSites.RegisterSite("MainToolStripPanelSiteName", 
             Me.Shell.InnerToolStripContainer.TopToolStripPanel)

This code registers the top panel, but you can register another or all of them, but only on separate sites. Once you have registered the site, you can create and add ToolStrips to it:

Dim objToolStip As New System.Windows.Forms.ToolStrip
LocalWorkItem.UIExtensionSites("MainToolStripPanelSiteName").Add(objToolStip)
LocalWorkItem.UIExtensionSites.RegisterSite("MyCustomToolStripSitename", _
                                            objToolStip)

Don't forget the second registration that allows you to add a button to the ToolStrip.

All done... You should now be able to create dynamic tool strips and populate them. If you want to customise a command, you will need to create a command adapter for the ToolStripPanel and add it to CAB.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Martin Hinshelwood

Instructor / Trainer
Northwest Cadence
United States United States

Member

Follow on Twitter Follow on Twitter

Martin Hinshelwood is an Senior ALM Consultant at Northwest Cadence, but also a Microsoft Visual Studio ALM MVPs in Washington, US (Formally UK) and has over 9 years experience in the software industry. He is a member of the Visual Studio ALM Rangers and is also a Professional Scrum Trainer. He regularly writes on his Processes, Practices & Tools blog, and speaks often on Scrum, good practices and Visual Studio ALM.

 

Martin aims to provide Strategic and Tactical consulting on successful implementations of new Processes, Practices & Tools within both small and large organisations across the world. These would include, but not be limited to:

 
  • Scrum Mentoring & Training - Scrum Master Training, Scrum Developer Training, Scrum Mentoring, Scrum Coaching
  •  
  • Practice improvements – Requirements (Backlog,Limiting Work In Progress, Relative Complexity, Acceptance Criteria, Planning Poker, User Stories, Use Cases, Visualisation) , Teams (Team Protection, Commitment), Code (Unit Testing, Branching, Continuous Integration, TDD, ), Test (Acceptance Test Driven Development, Automation, ), Release & Configuration (Continuous Delivery, Continuous Deployment)
  •  
  • Visual Studio ALM Implementation, upgrades & Training - Visual Studio, Team Foundation Server, Team Foundation Build & Microsoft Test Manager


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMistake Pinmembernayato23:31 17 Jan '07  
GeneralRe: Mistake PinmemberEdward Wilson7:05 14 May '07  
GeneralCAB? CAB! Pinmembermav.northwind4:15 26 Jun '06  
GeneralRe: CAB? CAB! Pinmemberpointnetsolutions9:41 26 Jun '06  
GeneralRe: CAB? CAB! Pinmemberhinshelwm10:11 26 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Jun 2006
Article Copyright 2006 by Martin Hinshelwood
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid