Creating an rxCustomUI object with local dispatch scope in VBA

From Ribbon Commander Documentation
Revision as of 22:09, 14 March 2013 by Rxdff15551 bb53 (Talk | contribs) (Code Analysis)

Jump to: navigation, search

Prerequisites

We recommend you go through Creating our first button in VBA before getting into this example.

Background

In previous examples we worked with the default rxCustomUI instance of the current session. This is fine for example code, but in real life it is preferable that each add-in/application creates its own context (or virtual add-in) to isolate its UI from other add-ins/applications.

The framework currently supports up to 1000 contexts/session so there are enough contexts to go around!

In VBA you can create rxCustomUI objects with two types of dispatch scopes (see rxCustomUI.create, make_delegate):

  • Global dispatch. Intended to be used with VBA standard modules. Delegates created by the rxCustomUI object dispatch to public functions in standard VBA modules. The rxCustomUI object returned by the static function rxCustomUI.defaultInstance has global dispatch.
  • Local context. Intended to be used with VBA classes. Delegates created by the rxCustomUI object dispatch to public methods of a user-supplied dispatch object, usually the owning class.

Creating a new local context with a drop down

  • Add a new class to your VBA project and name it clsMyLocalContext
  • Enter the following code to the class module
  1. Private m_customUI As rxCustomUI
  2.  
  3.  
  4. Private Sub Class_Initialize()
  5.     ' Create a new local context
  6.     Set m_customUI = rxCustomUI.Create("my_local_context", "My Local Context", DispatchScope_local)
  7.     ' Make the new context dispatch callbacks to member methods of this class
  8.     Set m_customUI.dispatchObject_weakRef = Me
  9.     ' Initialize the context's UI
  10.     CreateUI
  11. End Sub
  12.  
  13.  
  14. ' Initializes the context's UI
  15. Private Sub CreateUI()
  16.  
  17.     With m_customUI
  18.         .Clear
  19.  
  20.         ' Create a new tab
  21.         With .ribbon.tabs.Add(New rxTab)
  22.             .Label = "Local Ctx Tab"
  23.  
  24.             ' Create a new group
  25.             With .groups.Add(New rxGroup)
  26.                 .Label = "Local Ctx Group"
  27.  
  28.                 ' Create a new drop down
  29.                 With .DropDowns.Add(New rxDropDownRegular)
  30.                     .Label = "Local Ctx Drop-down"
  31.  
  32.                     ' Add a few items to the dropdown
  33.                     .items.Add(New rxItem).Label = "Item1"
  34.                     .items.Add(New rxItem).Label = "Item2"
  35.                     .items.Add(New rxItem).Label = "Item3"
  36.                     .items.Add(New rxItem).Label = "Item4"
  37.                     .items.Add(New rxItem).Label = "Item5"
  38.  
  39.                     .OnAction = m_customUI.make_delegate("MyDropdownOnAction")
  40.                 End With
  41.             End With
  42.         End With
  43.  
  44.         ' Render the UI
  45.         .Refresh
  46.     End With
  47.  
  48.  
  49. End Sub
  50.  
  51. Sub MyDropdownOnAction(ByVal control As DynamicRibbonX.IRibbonControl, itemID As String, ByVal itemIndex As Long)
  52.     MsgBox "Item @index " & itemIndex & " clicked"
  53. End Sub
  • Add a new standard module to your VBA project and name it mMyLocalContext
  • Enter the following code to the module
  1. Private myContext As clsMyLocalContext
  2.  
  3. Public Sub ShowLocalContextUI()
  4.     Set myContext = New clsMyLocalContext
  5. End Sub
  • Run sub ShowLocalContextUI to create a new drop-down labeled Local Ctx Tab in its own local context:

LocalCtxDropDown.png

Code Analysis

  1. Private m_customUI As rxCustomUI

A member rxCustomUI variable. Notice that the m_customUI is owned by the class.

  1. Private Sub Class_Initialize()
  2.     ' Create a new local context
  3.     Set m_customUI = rxCustomUI.Create("my_local_context", "My Local Context", DispatchScope_local)
  4.     ' Make the new context dispatch callbacks to member methods of this class
  5.     Set m_customUI.dispatchObject_weakRef = Me
  6.     ' Initialize the context's UI
  7.     CreateUI
  8. End Sub

This is the constructor of our class. Here we

  • Instantiate member m_customUI into a new context with contextId 'my_local_context' and with global dispatch.
  • Designate our class as the dispatch object for m_customUI. This is convenient as we can have both UI construction code and delegate callback code in the same class, but we need to use method dispatchObject_weakRef instead of dispatchObject to avoid creating a circular reference.