Difference between revisions of "Creating our first button in VBA"
From Ribbon Commander Documentation
(→Adding a handler for the button's onAction delegate) |
|||
Line 7: | Line 7: | ||
<syntaxhighlight lang="vb" line> | <syntaxhighlight lang="vb" line> | ||
Public Sub CreateMyUI3() | Public Sub CreateMyUI3() | ||
− | + | ||
− | + | Dim myCustomUI As rxCustomUI | |
+ | Set myCustomUI = rxCustomUI.defaultInstance | ||
+ | |||
+ | With myCustomUI | ||
' Clear old state | ' Clear old state | ||
.Clear | .Clear | ||
Line 22: | Line 25: | ||
With .Buttons.Add(New rxButton) | With .Buttons.Add(New rxButton) | ||
.Label = "My Button" | .Label = "My Button" | ||
− | |||
End With | End With | ||
Line 38: | Line 40: | ||
*: [[image: FirstButton.png]] | *: [[image: FirstButton.png]] | ||
− | == Code Analysis == | + | == Code Analysis I == |
<syntaxhighlight lang="vb"> | <syntaxhighlight lang="vb"> | ||
' Add a new group to our tab | ' Add a new group to our tab | ||
Line 86: | Line 88: | ||
.Label = "My Button" | .Label = "My Button" | ||
' Hook up the control's onAction delegate | ' Hook up the control's onAction delegate | ||
− | . | + | .onAction = myCustomUI.defaultInstance.make_delegate("MyCallback") |
End With | End With | ||
Line 119: | Line 121: | ||
* If everything has gone according to plan, clicking the button will now display: | * If everything has gone according to plan, clicking the button will now display: | ||
*: [[image: HelloWorldMsgBoxVBA.png]] | *: [[image: HelloWorldMsgBoxVBA.png]] | ||
+ | |||
+ | == Code Analysis I == | ||
+ | <syntaxhighlight lang="vb" line start="14" highlight="18"> | ||
+ | ' Add a new button to our group | ||
+ | With .Buttons.Add(New rxButton) | ||
+ | .Label = "My Button" | ||
+ | ' Hook up the control's onAction delegate | ||
+ | .onAction = myCustomUI.defaultInstance.make_delegate("MyCallback") | ||
+ | End With | ||
+ | </syntaxhighlight> | ||
== Notes == | == Notes == |
Revision as of 18:57, 10 March 2013
Contents
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
Public Sub CreateMyUI3()
Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.defaultInstance
With myCustomUI
' Clear old state
.Clear
' Add a new tab
With .ribbon.tabs.Add(New rxTab)
.Label = "My First Tab"
' Add a new group to our tab
With .groups.Add(New rxGroup)
.Label = "My Group"
' Add a new button to our group
With .Buttons.Add(New rxButton)
.Label = "My Button"
End With
End With
End With
' Render the UI
.Refresh
End With
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')
Code Analysis I
' 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
Public Sub CreateMyUI3()
Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.defaultInstance
With myCustomUI
' Clear old state
.Clear
' Add a new tab
With .ribbon.tabs.Add(New rxTab)
.Label = "My First Tab"
' Add a new group to our tab
With .groups.Add(New rxGroup)
.Label = "My Group"
' Add a new button to our group
With .Buttons.Add(New rxButton)
.Label = "My Button"
' Hook up the control's onAction delegate
.onAction = myCustomUI.defaultInstance.make_delegate("MyCallback")
End With
End With
End With
' Render the UI
.Refresh
End With
End Sub
- We have now hooked up the control's onAction delegate with a function called MyCallback, which we haven't yet provided.
- We can now either manually provide a delegate stub function with the correct name and signature, or make use of the Dynamic RibbonX VBA tools to insert it automatically:
Sub MyCallback(ByVal control As DynamicRibbonX.IRibbonControl) Err.Raise &H80004001, , "Not implemented" End Sub
- Run the updated sub (CreateMyUI3).
- The delegate stub currently raises and error. Update it to display a message box instead:
Sub MyCallback(ByVal control As DynamicRibbonX.IRibbonControl) MsgBox "Hello, world!" End Sub
Code Analysis I
' Add a new button to our group
With .Buttons.Add(New rxButton)
.Label = "My Button"
' Hook up the control's onAction delegate
.onAction = myCustomUI.defaultInstance.make_delegate("MyCallback")
End With