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.​

Boxit Document Solutions Logo

Boxit Document Solutions

Trayport Logo

Trayport

Sage Logo

Sage

Deliotte Logo

Deliotte

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

Epic Games Logo

Epic Games

Workday Logo

Workday

Slicedbread Logo

Slicedbread

Higher Education Statistics Agency Logo

Higher Education Statistics Agency

Bistech Logo

Bistech

Graham & Brown Logo

Graham & Brown

YearUp.org Logo

YearUp.org

Qualco Logo

Qualco

Slaughter and May Logo

Slaughter and May

Freadom Logo

Freadom

Hubtel Ghana Logo

Hubtel Ghana

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Big Data for Humans Logo

Big Data for Humans

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Ghana Police Service Logo

Ghana Police Service

Nottingham County Council Logo

Nottingham County Council

Royal Air Force Logo

Royal Air Force

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

ProgramUtvikling Logo

ProgramUtvikling

SuperControl Logo

SuperControl

Philips Logo

Philips

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Kongsberg Maritime Logo

Kongsberg Maritime