Creating an rxCustomUI object with global dispatch scope in VBA

From Ribbon Commander Documentation
Revision as of 11:00, 21 October 2013 by Rxdff15551 bb53 (Talk | contribs) (Creating a new global context with a check box)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 global context with a check box

  • Enter the code below in a standard module
  1. Public Sub GlobalContextUI()
  2.  
  3.     ' Create an rxCustomUI in a global context
  4.     Dim myCustomUI As rxCustomUI
  5.     Set myCustomUI = rxCustomUI.create("my_sample_context", "My Global Context", DispatchScope_global)
  6.  
  7.     With myCustomUI
  8.         .Clear
  9.  
  10.         With .ribbon.tabs.Add(New rxTab)
  11.             .Label = "Global Ctx Tab"
  12.             With .groups.Add(New rxGroup)
  13.                 .Label = "Global Ctx Group"
  14.                 With .CheckBoxes.Add(New rxCheckBox)
  15.                     .Label = "Global Ctx Checkbox"
  16.                     .OnAction = myCustomUI.make_delegate("MyGlobalCtxCheckBoxCallback")
  17.                 End With
  18.             End With
  19.         End With
  20.  
  21.         .Refresh
  22.     End With
  23.  
  24. End Sub
  25.  
  26. Sub MyGlobalCtxCheckBoxCallback(ByVal control As RibbonCommander.IRibbonControl, pressed As Boolean)
  27.     MsgBox "Global Ctx Checkbox " & IIf(pressed, "clicked", "un-clicked")
  28. End Sub
  • Run sub GlobalContextUI to create a new check box labeled Global Ctx Checkbox in its own global context.

GlobalCtxCheckBox.png

  • If everything has gone according to plan, the following message box will appear:

GlobalCtxMsgBox.png

Code Analysis

The above code should be pretty much familiar (see Creating our first button in VBA, instead of a button we are using a check box in this example) apart from the lines below:

  1. ' Create an rxCustomUI in a global context
  2. Dim myCustomUI As rxCustomUI
  3. Set myCustomUI = rxCustomUI.create("my_sample_context", "My Global Context", DispatchScope_global)

Here we create a new rxCustomUI with global dispatch scope through rxCustomUI's static function create with the following arguments:

  • contextId == "my_sample_context". An id string that uniquely identifies this context. Other add-ins/applications can access this same context by creating a context with the same id.
  • description == "My Global Context". This is the context's description. It appears when a user hovers over one of the context's controls:
    GlobalCtxAddinDescription.png
  • dispatchScope == DispatchScope_global. Global dispatch scope