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

From Ribbon Commander Documentation
Jump to: navigation, search
(Creating a new local context with a drop down)
(Creating a new local context with a drop down)
Line 7: Line 7:
 
== Creating a new local context with a drop down ==
 
== Creating a new local context with a drop down ==
 
* Create a new class module and name it ''clsMyLocalContext''
 
* Create a new class module and name it ''clsMyLocalContext''
 +
* Enter the following code to the class
 +
<syntaxhighlight lang="vb" line>
 +
Private m_customUI As rxCustomUI
 +
 +
 +
Private Sub Class_Initialize()
 +
    ' Create a new local context
 +
    Set m_customUI = rxCustomUI.Create("my_local_context", "My Local Context", DispatchScope_local)
 +
    ' Make the new context dispatch callbacks to member methods of this class
 +
    Set m_customUI.dispatchObject_weakRef = Me
 +
    ' Initialize the context's UI
 +
    CreateUI
 +
End Sub
 +
 +
 +
' Initializes the context's UI
 +
Private Sub CreateUI()
 +
   
 +
    With m_customUI
 +
        .Clear
 +
   
 +
        ' Create a new tab
 +
        With .ribbon.tabs.Add(New rxTab)
 +
            .Label = "Local Ctx Tab"
 +
       
 +
            ' Create a new group
 +
            With .groups.Add(New rxGroup)
 +
                .Label = "Local Ctx Group"
 +
           
 +
                ' Create a new drop down
 +
                With .DropDowns.Add(New rxDropDownRegular)
 +
                    .Label = "Local Ctx Drop-down"
 +
                   
 +
                    ' Add a few items to the dropdown
 +
                    .items.Add(New rxItem).Label = "Item1"
 +
                    .items.Add(New rxItem).Label = "Item2"
 +
                    .items.Add(New rxItem).Label = "Item3"
 +
                    .items.Add(New rxItem).Label = "Item4"
 +
                    .items.Add(New rxItem).Label = "Item5"
 +
                   
 +
                    .OnAction = m_customUI.make_delegate("MyDropdownOnAction")
 +
                End With
 +
            End With
 +
        End With
 +
       
 +
        ' Render the UI
 +
        .Refresh
 +
    End With
 +
   
 +
   
 +
End Sub
 +
 +
Sub MyDropdownOnAction(ByVal control As DynamicRibbonX.IRibbonControl, itemID As String, ByVal itemIndex As Long)
 +
    MsgBox "Item @index " & itemIndex & " clicked"
 +
End Sub
 +
</syntaxhighlight>

Revision as of 20:36, 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 local context with a drop down

  • Create a new class module and name it clsMyLocalContext
  • Enter the following code to the class
  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