Difference between revisions of "Creating our first button in VBA"

From Ribbon Commander Documentation
Jump to: navigation, search
(Adding a handler for the button's onAction delegate)
(Adding a handler for the button's onAction delegate)
Line 67: Line 67:
 
<syntaxhighlight lang="vb" line highlight="18">
 
<syntaxhighlight lang="vb" line highlight="18">
 
Public Sub CreateMyUI3()
 
Public Sub CreateMyUI3()
 
+
   
     With rxCustomUI.defaultInstance
+
     Dim myCustomUI As rxCustomUI
 +
    Set myCustomUI = rxCustomUI.defaultInstance
 +
   
 +
    With myCustomUI
 
         ' Clear old state
 
         ' Clear old state
 
         .Clear
 
         .Clear
Line 83: Line 86:
 
                     .Label = "My Button"
 
                     .Label = "My Button"
 
                     ' Hook up the control's onAction delegate
 
                     ' Hook up the control's onAction delegate
                     .onAction = rxCustomUI.defaultInstance.make_delegate("MyCallback")                  
+
                     .OnAction = myCustomUI.defaultInstance.make_delegate("MyCallback")
 
                 End With
 
                 End With
 
                  
 
                  

Revision as of 18:31, 10 March 2013

Prerequisites

We recommend you go though A 'hello world' VBA program before going into this example.

Creating a button

  • Enter the code below in a standard VBA module
  1. Public Sub CreateMyUI3()
  2.  
  3.     With rxCustomUI.defaultInstance
  4.         ' Clear old state
  5.         .Clear
  6.  
  7.         ' Add a new tab
  8.         With .ribbon.tabs.Add(New rxTab)
  9.             .Label = "My First Tab"
  10.             ' Add a new group to our tab
  11.             With .groups.Add(New rxGroup)
  12.                 .Label = "My Group"
  13.  
  14.                 ' Add a new button to our group
  15.                 With .Buttons.Add(New rxButton)
  16.                     .Label = "My Button"
  17.  
  18.                 End With
  19.  
  20.             End With
  21.         End With
  22.  
  23.         ' Render the UI
  24.         .Refresh
  25.     End With
  26.  
  27. End Sub
  • Run the sub to create a button labeled 'My Button' (which belongs to a group labeled 'My Group' which, in turn' belongs to a tab labeled 'My Tab')
    FirstButton.png

Code Analysis

' Add a new group to our tab
With .groups.Add(New rxGroup)
    .Label = "My Group"
    '...
End With

Each rxTab object has a collection of rxGroup objects (accessible through its .groups property). Here we add a new group to our tab's groups and label it 'My Group'


' Add a new button to our group
With .Buttons.Add(New rxButton)
    .Label = "My Button"
 
End With

Each rxGroup object has a collection of rxButton objects (accesible through its .buttons property). Here we add a new button to out group's buttons and label it 'My Button'


Adding a handler for the button's onAction delegate

At the moment the new button isn't of much use as it does nothing when clicked. In this section we will hook up to the control's onAction delegate.

  • Update the sub as follows
  1. Public Sub CreateMyUI3()
  2.  
  3.     Dim myCustomUI As rxCustomUI
  4.     Set myCustomUI = rxCustomUI.defaultInstance
  5.  
  6.     With myCustomUI
  7.         ' Clear old state
  8.         .Clear
  9.  
  10.         ' Add a new tab
  11.         With .ribbon.tabs.Add(New rxTab)
  12.             .Label = "My First Tab"
  13.             ' Add a new group to our tab
  14.             With .groups.Add(New rxGroup)
  15.                 .Label = "My Group"
  16.  
  17.                 ' Add a new button to our group
  18.                 With .Buttons.Add(New rxButton)
  19.                     .Label = "My Button"
  20.                     ' Hook up the control's onAction delegate
  21.                     .OnAction = myCustomUI.defaultInstance.make_delegate("MyCallback")
  22.                 End With
  23.  
  24.             End With
  25.         End With
  26.  
  27.         ' Render the UI
  28.         .Refresh
  29.     End With
  30.  
  31. End Sub

Notes