tech·nic·al·ly agile

Adding ToolStripPanel UI Adapter Support to the Composite UI Application Block

Learn how to enhance the Composite UI Application Block with ToolStripPanel support. Discover code solutions and best practices for UI adaptation.

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

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
Comments

Related blog posts

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

Genus Breeding Ltd Logo
MacDonald Humfrey (Automation) Ltd. Logo
Kongsberg Maritime Logo
Teleplan Logo
Deliotte Logo
Xceptor - Process and Data Automation Logo
Qualco Logo
Brandes Investment Partners L.P. Logo
Philips Logo
Boxit Document Solutions Logo
Bistech Logo
Jack Links Logo
Illumina Logo
Slaughter and May Logo
Workday Logo
Ericson Logo
Freadom Logo
Big Data for Humans Logo
Royal Air Force Logo
Washington Department of Transport Logo
Washington Department of Enterprise Services Logo
New Hampshire Supreme Court Logo
Department of Work and Pensions (UK) Logo
Ghana Police Service Logo

NIT A/S

DFDS Logo
Sage Logo
ALS Life Sciences Logo
Healthgrades Logo
ProgramUtvikling Logo