Difference between revisions of "Creating an rxCustomUI object with global dispatch scope in VBA"

From Ribbon Commander Documentation
Jump to: navigation, search
(Creating a new global context with a check box)
Line 7: Line 7:
 
== Creating a new global context with a check box ==
 
== Creating a new global context with a check box ==
 
* Enter the code below in a standard module
 
* Enter the code below in a standard module
<codehighlight lang="vb" line>
+
<syntaxhighlight lang="vb" line>
 
Public Sub GlobalContextUI()
 
Public Sub GlobalContextUI()
 
      
 
      
Line 35: Line 35:
 
     MsgBox "Global Ctx Checkbox " & IIf(pressed, "clicked", "un-clicked")
 
     MsgBox "Global Ctx Checkbox " & IIf(pressed, "clicked", "un-clicked")
 
End Sub
 
End Sub
</codehighlight>
+
</syntaxhighlight>
 
* Run sub '''GlobalContextUI''' to create a new check box labeled ''Global Ctx Checkbox'' in it's own global context.
 
* Run sub '''GlobalContextUI''' to create a new check box labeled ''Global Ctx Checkbox'' in it's own global context.

Revision as of 19:52, 10 March 2013

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.     Dim myCustomUI As rxCustomUI
  4.     Set myCustomUI = rxCustomUI.Create("my_sample_context", "My Global Context", DispatchScope_global)
  5.  
  6.     With myCustomUI
  7.         .Clear
  8.  
  9.         With .ribbon.tabs.Add(New rxTab)
  10.             .Label = "Global Ctx Tab"
  11.             With .groups.Add(New rxGroup)
  12.                 .Label = "Global Ctx Group"
  13.                 With .CheckBoxes.Add(New rxCheckBox)
  14.                     .Label = "Global Ctx Checkbox"
  15.                     .OnAction = myCustomUI.make_delegate("MyGlobalCtxCheckBoxCallback")
  16.                 End With
  17.             End With
  18.         End With
  19.  
  20.         .Refresh
  21.     End With
  22.  
  23. End Sub
  24.  
  25. Sub MyGlobalCtxCheckBoxCallback(ByVal control As DynamicRibbonX.IRibbonControl, pressed As Boolean)
  26.     MsgBox "Global Ctx Checkbox " & IIf(pressed, "clicked", "un-clicked")
  27. End Sub
  • Run sub GlobalContextUI to create a new check box labeled Global Ctx Checkbox in it's own global context.