a·gen·tic a·gil·i·ty

Adding ToolStripPanel UI Adapter Support to the Composite UI Application Block

Explains how to extend the Composite UI Application Block to support ToolStripPanel UI adapters, enabling dynamic ToolStrip integration and customisation in CAB applications.

Published on
3 minute read
Image
https://nkdagility.com/resources/y_vlPh65_5Q
Subscribe

I was very surprised to find that CAB did not support a higher-level component like the ToolStrip in its basic implementation. I resolved to fix this and added an adapter and factory for the ToolStripPanel initially, but I encountered issues with the ToolStripContainer. As my first post, here is the ToolStripPanel code.

There are two parts to this and two ways to implement it. The first option is to edit the existing ToolStrip UI factory to include the new code, but I prefer not to do this as future CAB updates might require re-implementing the adaptations.

The second option is to inherit from the current classes in your own assembly and adapt the class to support both. This way, you extend the functionality without replacing it.

Here is the code for the new ToolStripPanelUIAdapterFactory factory:

 1Public Class ToolStripPanelUIAdapter
 2    Inherits UIElementAdapter(Of ToolStrip)
 3
 4    Private innerToolStripPanel As ToolStripPanel
 5
 6    Public Sub New(ByVal objToolStripPanel As ToolStripPanel)
 7        Guard.ArgumentNotNull(objToolStripPanel, "objToolStripPanel")
 8        Me.innerToolStripPanel = objToolStripPanel
 9    End Sub
10
11    Protected Overrides Function Add(ByVal uiElement As ToolStrip) As ToolStrip
12        If Me.innerToolStripPanel Is Nothing Then
13            Throw New InvalidOperationException()
14        End If
15        Me.innerToolStripPanel.Join(uiElement, 3)
16        Return uiElement
17    End Function
18
19    Protected Overrides Sub Remove(ByVal uiElement As ToolStrip)
20        If Me.innerToolStripPanel.Controls.Contains(uiElement) Then
21            Me.innerToolStripPanel.Controls.Remove(uiElement)
22        End If
23    End Sub
24End Class

As you can see, this class inherits from the CAB class of the same name, overrides the same methods, and adds support for the new adapter for the ToolStripPanel.

Now, you can add the factory to the UI Element Adapter Factory Catalog in the AfterShellCreated part of the main shell application:

1Dim catalog As IUIElementAdapterFactoryCatalog = RootWorkItem.Services.Get(Of IUIElementAdapterFactoryCatalog)()
2catalog.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 ToolStripPanelAdapter:

 1Public Class ToolStripPanelUIAdapter
 2    Inherits UIElementAdapter(Of ToolStrip)
 3
 4    Private innerToolStripPanel As ToolStripPanel
 5    Private Shared NumberOfAddedControls As Integer = 0
 6
 7    Public Sub New(ByVal objToolStripPanel As ToolStripPanel)
 8        Guard.ArgumentNotNull(objToolStripPanel, "objToolStripPanel")
 9        Me.innerToolStripPanel = objToolStripPanel
10    End Sub
11
12    Protected Overrides Function Add(ByVal uiElement As ToolStrip) As ToolStrip
13        If Me.innerToolStripPanel Is Nothing Then
14            Throw New InvalidOperationException()
15        End If
16        Dim Row As Integer = 2
17        ' TODO: work out how to specify row!
18        Me.innerToolStripPanel.Join(uiElement, 0)
19        Me.innerToolStripPanel.Controls.SetChildIndex(uiElement, NumberOfAddedControls + 1)
20        NumberOfAddedControls += 1
21        Return uiElement
22    End Function
23
24    Protected Overrides Sub Remove(ByVal uiElement As ToolStrip)
25        If Me.innerToolStripPanel.Controls.Contains(uiElement) Then
26            Me.innerToolStripPanel.Controls.Remove(uiElement)
27        End If
28    End Sub
29End Class

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

1Dim objToolStrip As New System.Windows.Forms.ToolStrip
2LocalWorkItem.UIExtensionSites("MainToolStripPanelSiteName").Add(objToolStrip)
3LocalWorkItem.UIExtensionSites.RegisterSite("MyCustomToolStripSitename", objToolStrip)

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 customize commands, you will need to create a command adapter for the ToolStripPanel and add it to CAB.

Software Development
Subscribe

Related Blog

No related videos found.

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Higher Education Statistics Agency Logo

Higher Education Statistics Agency

Microsoft Logo

Microsoft

Qualco Logo

Qualco

Epic Games Logo

Epic Games

Emerson Process Management Logo

Emerson Process Management

Boeing Logo

Boeing

YearUp.org Logo

YearUp.org

SuperControl Logo

SuperControl

Boxit Document Solutions Logo

Boxit Document Solutions

Jack Links Logo

Jack Links

Philips Logo

Philips

Teleplan Logo

Teleplan

Ericson Logo

Ericson

Big Data for Humans Logo

Big Data for Humans

Akaditi Logo

Akaditi

Bistech Logo

Bistech

Lean SA Logo

Lean SA

DFDS Logo

DFDS

Nottingham County Council Logo

Nottingham County Council

Washington Department of Transport Logo

Washington Department of Transport

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Ghana Police Service Logo

Ghana Police Service

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

Royal Air Force Logo

Royal Air Force

Sage Logo

Sage

Lockheed Martin Logo

Lockheed Martin

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Microsoft Logo

Microsoft

Teleplan Logo

Teleplan

MacDonald Humfrey (Automation) Ltd. Logo

MacDonald Humfrey (Automation) Ltd.