Click here to Skip to main content
6,306,412 members and growing! (19,433 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Intermediate License: The Microsoft Public License (Ms-PL)

ToolStripPanel UIAdapter

By Martin Hinshelwood

Adding ToolStripPanel support to the Composite UI Application Block
VB, Windows, .NET, Visual Studio, Dev
Posted:25 Jun 2006
Views:26,060
Bookmarked:22 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
5 votes for this article.
Popularity: 2.53 Rating: 3.62 out of 5
1 vote, 20.0%
1
1 vote, 20.0%
2
1 vote, 20.0%
3

4
2 votes, 40.0%
5

Introduction

I was very supprised to find that CAB did not support a higher level the the ToolStrip in its basic implementation. I resolved to solve this and I have added an adapter and factory for the ToolStripPanel initialy, 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 havign to implement the adaptions again.

The second is to inherit from the current classes in you 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 ToolStripPanel's as UI Extention Sites with the appropriet 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 the 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 seperate sites. Once you have register 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 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


Member

Martin has been involved with computers in one way or another for as long as he can remember, but started professionally in 2000. After university Martin started out as an ASP 3.0 developer for a year working with Access and VBScript, but he breathed a sigh of relief when the .NET Framework was released in 2001 and has been working with .NET and VB ever since.



Occupation: Software Developer
Location: United Kingdom United Kingdom

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMistake Pinmembernayato0:31 18 Jan '07  
GeneralRe: Mistake PinmemberEdward Wilson8:05 14 May '07  
GeneralCAB? CAB! Pinmembermav.northwind5:15 26 Jun '06  
GeneralRe: CAB? CAB! Pinmemberpointnetsolutions10:41 26 Jun '06  
GeneralRe: CAB? CAB! Pinmemberhinshelwm11:11 26 Jun '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jun 2006
Editor:
Copyright 2006 by Martin Hinshelwood
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project