Difference between revisions of "Using events in VBA"

From Ribbon Commander Documentation
Jump to: navigation, search
(Created page with "== Prerequisites == We recommend you go though Creating an rxCustomUI object with local dispatch scope in VBA before going into this example. == Introduction == All Dynam...")
 
(Subscribing to control events)
 
(12 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
 
== Introduction ==
 
== Introduction ==
All Dynamic RibbonX controls that model UI elements with delegates support VBA events. [[rxDispatchScope | Local dispatch scope]] is most appropriate for use in VBA classes.
+
All Ribbon Commander 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 VBA classes. Since we are using events instead of delegates, the [[rxDispatchScope | 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
 +
<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_events_demo_ui", "My Events Demo UI")
 +
    ' 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 = "Events Demo Tab"
 +
 +
            ' Create a new group
 +
            With .groups.Add(New rxGroup)
 +
                .label = "Events Demo 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>
 +
* Use the VBA environment to insert event stubs for member ''m_button''
 +
[[Image:InsertingEventStubVBA.png|link=]]
 +
 
 +
Below we have inserted all event stubs for ''m_button'' and have added implementation code to them:
 +
<syntaxhighlight lang="vb" line highlight="2">
 +
Private Sub m_button_OnActionEvent(ByVal control As RibbonCommander.IRibbonControl)
 +
    MsgBox "Button clicked!"
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetDescription(ByVal control As RibbonCommander.IRibbonControl, description As String)
 +
    description = "My button description"
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetEnabled(ByVal control As RibbonCommander.IRibbonControl, enabled As Boolean)
 +
    enabled = True
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetImage(ByVal control As RibbonCommander.IRibbonControl, imageDispOrImageMso As Variant)
 +
    imageDispOrImageMso = "ChartInsert"
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetKeytip(ByVal control As RibbonCommander.IRibbonControl, keytip As String)
 +
    keytip = "K"
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetLabel(ByVal control As RibbonCommander.IRibbonControl, label As String)
 +
    label = "Click me!"
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetScreentip(ByVal control As RibbonCommander.IRibbonControl, screentip As String)
 +
    screentip = "My Button Screentip"
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetShowImage(ByVal control As RibbonCommander.IRibbonControl, showImage As Boolean)
 +
    showImage = True
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetShowLabel(ByVal control As RibbonCommander.IRibbonControl, showLabel As Boolean)
 +
    showLabel = True
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetSize(ByVal control As RibbonCommander.IRibbonControl, size As RibbonCommander.RibbonControlSize)
 +
    size = RibbonControlSizeLarge
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetSupertip(ByVal control As RibbonCommander.IRibbonControl, supertip As String)
 +
    supertip = "My Button Supertip"
 +
End Sub
 +
 
 +
Private Sub m_button_OnGetVisible(ByVal control As RibbonCommander.IRibbonControl, visible As Boolean)
 +
    visible = True
 +
End Sub
 +
</syntaxhighlight>
 +
 
 +
* Add a new standard module to your VBA project and name it ''mEventsDemoUI''
 +
* Enter the following code to the module
 +
<syntaxhighlight lang="vb" line>
 +
Private myContext As clsEventsDemoUI
 +
 +
Public Sub ShowEventsDemoUI()
 +
    Set myContext = New clsEventsDemoUI
 +
End Sub
 +
</syntaxhighlight>
 +
* Run sub ''ShowEventsDemoUI''. If everything went according to plan the below UI will appear:
 +
[[image: EventDemoUIVBA.png|link=]]

Latest revision as of 11:08, 12 January 2015

Prerequisites

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

Introduction

All Ribbon Commander 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 VBA classes. 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_events_demo_ui", "My Events Demo UI")
  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 = "Events Demo Tab"
  21.  
  22.             ' Create a new group
  23.             With .groups.Add(New rxGroup)
  24.                 .label = "Events Demo 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
  • Use the VBA environment to insert event stubs for member m_button

InsertingEventStubVBA.png

Below we have inserted all event stubs for m_button and have added implementation code to them:

  1. Private Sub m_button_OnActionEvent(ByVal control As RibbonCommander.IRibbonControl)
  2.     MsgBox "Button clicked!"
  3. End Sub
  4.  
  5. Private Sub m_button_OnGetDescription(ByVal control As RibbonCommander.IRibbonControl, description As String)
  6.     description = "My button description"
  7. End Sub
  8.  
  9. Private Sub m_button_OnGetEnabled(ByVal control As RibbonCommander.IRibbonControl, enabled As Boolean)
  10.     enabled = True
  11. End Sub
  12.  
  13. Private Sub m_button_OnGetImage(ByVal control As RibbonCommander.IRibbonControl, imageDispOrImageMso As Variant)
  14.     imageDispOrImageMso = "ChartInsert"
  15. End Sub
  16.  
  17. Private Sub m_button_OnGetKeytip(ByVal control As RibbonCommander.IRibbonControl, keytip As String)
  18.     keytip = "K"
  19. End Sub
  20.  
  21. Private Sub m_button_OnGetLabel(ByVal control As RibbonCommander.IRibbonControl, label As String)
  22.     label = "Click me!"
  23. End Sub
  24.  
  25. Private Sub m_button_OnGetScreentip(ByVal control As RibbonCommander.IRibbonControl, screentip As String)
  26.     screentip = "My Button Screentip"
  27. End Sub
  28.  
  29. Private Sub m_button_OnGetShowImage(ByVal control As RibbonCommander.IRibbonControl, showImage As Boolean)
  30.     showImage = True
  31. End Sub
  32.  
  33. Private Sub m_button_OnGetShowLabel(ByVal control As RibbonCommander.IRibbonControl, showLabel As Boolean)
  34.     showLabel = True
  35. End Sub
  36.  
  37. Private Sub m_button_OnGetSize(ByVal control As RibbonCommander.IRibbonControl, size As RibbonCommander.RibbonControlSize)
  38.     size = RibbonControlSizeLarge
  39. End Sub
  40.  
  41. Private Sub m_button_OnGetSupertip(ByVal control As RibbonCommander.IRibbonControl, supertip As String)
  42.     supertip = "My Button Supertip"
  43. End Sub
  44.  
  45. Private Sub m_button_OnGetVisible(ByVal control As RibbonCommander.IRibbonControl, visible As Boolean)
  46.     visible = True
  47. End Sub
  • Add a new standard module to your VBA project and name it mEventsDemoUI
  • Enter the following code to the module
  1. Private myContext As clsEventsDemoUI
  2.  
  3. Public Sub ShowEventsDemoUI()
  4.     Set myContext = New clsEventsDemoUI
  5. End Sub
  • Run sub ShowEventsDemoUI. If everything went according to plan the below UI will appear:

EventDemoUIVBA.png