Difference between revisions of "Using events in VBA"

From Ribbon Commander Documentation
Jump to: navigation, search
(Introduction)
Line 8: Line 8:
 
* Add a new class to your VBA project and name it clsEventsDemoUI
 
* Add a new class to your VBA project and name it clsEventsDemoUI
 
* Enter the following code to the class module
 
* Enter the following code to the class module
 +
<syntaxhighlight lang="vb" line highlight="2">
 +
Private m_customUI As rxCustomUI
 +
Private WithEvents m_button As rxButton
 +
 +
Private Sub Class_Initialize()
 +
    ' Create a new local context
 +
    Set m_customUI = rxCustomUI.Create("my_local_context", "My Local Context", DispatchScope_local)
 +
    ' 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"
 +
               
 +
                ' Add a new button to the group and subscribe to its events
 +
                ' NOTE: By assigning the new rxButton object reference to the member variable
 +
                '      that has been declared 'WithEvents' we are subscribing to the control's
 +
                '      events
 +
                Set m_button = .Buttons.Add(New rxButton)
 +
            End With
 +
        End With
 +
 +
        ' Render the UI
 +
        .Refresh
 +
    End With
 +
 +
End Sub
 +
</syntaxhighlight>

Revision as of 17:10, 17 March 2013

Prerequisites

We recommend you go though Creating an rxCustomUI object with local dispatch scope in VBA before going into this example.

Introduction

All Dynamic RibbonX controls that model UI elements with delegates support VBA events. In order to subscribe to events in VBA we need to write our UI code in a VBA class. Since we are using events instead of delegates, the dispatch scope of our customUI is irrelevant.

Subscribing to control events

  • Add a new class to your VBA project and name it clsEventsDemoUI
  • Enter the following code to the class module
  1. Private m_customUI As rxCustomUI
  2. Private WithEvents m_button As rxButton
  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.     ' Initialize the context's UI
  8.     CreateUI
  9. End Sub
  10.  
  11.  
  12. ' Initializes the context's UI
  13. Private Sub CreateUI()
  14.  
  15.     With m_customUI
  16.         .Clear
  17.  
  18.         ' Create a new tab
  19.         With .ribbon.tabs.Add(New rxTab)
  20.             .label = "Local Ctx Tab"
  21.  
  22.             ' Create a new group
  23.             With .groups.Add(New rxGroup)
  24.                 .label = "Local Ctx Group"
  25.  
  26.                 ' Add a new button to the group and subscribe to its events
  27.                 ' NOTE: By assigning the new rxButton object reference to the member variable
  28.                 '       that has been declared 'WithEvents' we are subscribing to the control's
  29.                 '       events
  30.                 Set m_button = .Buttons.Add(New rxButton)
  31.             End With
  32.         End With
  33.  
  34.         ' Render the UI
  35.         .Refresh
  36.     End With
  37.  
  38. End Sub