Difference between revisions of "Method make delegate"
From Ribbon Commander Documentation
(→Remarks) |
(→Remarks) |
||
(26 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Description == | == Description == | ||
− | Creates a new [[rxDelegate]] object associated | + | Creates a new [[rxDelegate]] object associated with a callback function and owned by the [[rxCustomUI]] instance. |
== Parameters == | == Parameters == | ||
− | === | + | === rxCustomUI.make_delegate(string) -> rxDelegate === |
{{FuncTableBegin}} | {{FuncTableBegin}} | ||
{{FuncTableEntry | name=methodName | type=string | value=''[none]'' | desc=The name of the target callback function}} | {{FuncTableEntry | name=methodName | type=string | value=''[none]'' | desc=The name of the target callback function}} | ||
Line 9: | Line 9: | ||
{{FuncTableEnd}} | {{FuncTableEnd}} | ||
− | === . | + | === rxCustomUI.make_delegate(Delegate) -> rxDelegate === |
{{FuncTableBegin}} | {{FuncTableBegin}} | ||
{{FuncTableEntry | name=callbackDelegate | type=Delegate | value=''[none]'' | desc=An instance of a .NET delegate used to dispatch the callback}} | {{FuncTableEntry | name=callbackDelegate | type=Delegate | value=''[none]'' | desc=An instance of a .NET delegate used to dispatch the callback}} | ||
Line 17: | Line 17: | ||
== Remarks == | == Remarks == | ||
# (VBA-specific) If the [[rxCustomUI]] object has [[rxDispatchScope | global dispatch scope]], the target function needs to be a global member of a standard VBA module | # (VBA-specific) If the [[rxCustomUI]] object has [[rxDispatchScope | global dispatch scope]], the target function needs to be a global member of a standard VBA module | ||
− | # | + | # Otherwise the target function needs to be a public method of the object used as the [[rxCustomUI]]'s [[Property dispatchObject | dispatch object]]. |
+ | # (.NET-specific) By using the second method override, Native .NET delegates can be used to instantiate [[rxDelegate]] objects. There is a *Delegates class for every Ribbon Commander control in namespace LogismiX.DynamicRibbonX.Core that specifies the callback signature of every callback (see [[#Examples|Examples]]), e.g. class rxButtonDelegates contains all delegates of rxButton. | ||
== Examples == | == Examples == | ||
=== VBA === | === VBA === | ||
− | <syntaxhighlight lang="vb" line> | + | |
+ | ==== Global dispatch ==== | ||
+ | <syntaxhighlight lang="vb" line highlight="24"> | ||
+ | Public Sub MakeDelegateTest() | ||
+ | |||
+ | ' Create a new rxCustomUI with global dispatch scope | ||
+ | Dim myCustomUI As rxCustomUI | ||
+ | Set myCustomUI = rxCustomUI.Create("my_custom_context", , DispatchScope_global) | ||
+ | |||
+ | With myCustomUI | ||
+ | ' Clear old state | ||
+ | .Clear | ||
+ | |||
+ | ' Add new tab and label it 'My Tab' | ||
+ | With .ribbon.tabs.Add(New rxTab) | ||
+ | .Label = "My Tab" | ||
+ | ' Add new group and label it 'My Group' | ||
+ | With .groups.Add(New rxGroup) | ||
+ | |||
+ | ' Add a new button and label it 'Click Me!' | ||
+ | Dim myButton As rxButton | ||
+ | Set myButton = .Buttons.Add(New rxButton) | ||
+ | |||
+ | myButton.Label = "Click Me!" | ||
+ | |||
+ | ' Add a delegate that dispatches to a function with name 'MyCallbackFunc' | ||
+ | myButton.OnAction = myCustomUI.make_delegate("MyCallbackFunc") | ||
+ | |||
+ | End With | ||
+ | |||
+ | End With | ||
+ | |||
+ | 'Render the UI | ||
+ | .Refresh | ||
+ | |||
+ | End With | ||
+ | |||
+ | End Sub | ||
+ | |||
+ | ' The callback stub | ||
+ | Public Sub MyCallbackFunc(ByVal control As DynamicRibbonX.IRibbonControl) | ||
+ | MsgBox "Button clicked!" | ||
+ | End Sub | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | See also: [[Creating our first button in VBA]], [[Creating an rxCustomUI object with global dispatch scope in VBA]] | ||
+ | |||
+ | ==== Local dispatch ==== | ||
+ | <syntaxhighlight lang="vb" line highlight="27"> | ||
+ | ' | ||
+ | ' in class clsMyUI | ||
+ | ' | ||
+ | Private m_customUI As rxCustomUI | ||
+ | |||
+ | Private Sub Class_Initialize() | ||
+ | |||
+ | ' Instantiate a local dispatch rxCustomUI | ||
+ | Set m_customUI = rxCustomUI.Create("my_local_disp_ctxt", , DispatchScope_local) | ||
+ | |||
+ | With m_customUI | ||
+ | .Clear | ||
+ | |||
+ | ' IMPORTANT: We need to specify a dispatch object through which callbacks are dispatched | ||
+ | ' Here we have chosen to use the same class for the UI and the callbacks, which | ||
+ | ' is convenient but also means we need to use the *_weakRef version of the method | ||
+ | ' to avoid circular references. | ||
+ | Set .dispatchObject_weakRef = Me | ||
+ | |||
+ | With .ribbon.tabs.Add(New rxTab) | ||
+ | .Label = "Local Dispatch Tab" | ||
+ | With .groups.Add(New rxGroup) | ||
+ | .Label = "Local Dispatch Group" | ||
+ | |||
+ | With .Buttons.Add(New rxButton) | ||
+ | .Label = "Local Dispatch Button" | ||
+ | .OnAction = m_customUI.make_delegate("LocalDispCallback") | ||
+ | End With | ||
+ | |||
+ | End With | ||
+ | |||
+ | End With | ||
+ | |||
+ | .Refresh | ||
+ | End With | ||
+ | |||
+ | End Sub | ||
+ | |||
+ | ' The callback stub | ||
+ | ' NOTE: Notice it is a public method of the rxCustomUI's dispatch object, which happens to | ||
+ | ' be this class here | ||
+ | Sub LocalDispCallback(ByVal control As DynamicRibbonX.IRibbonControl) | ||
+ | MsgBox "Called through local dispatch!" | ||
+ | End Sub | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | See also: [[Creating an rxCustomUI object with local dispatch scope in VBA]] | ||
=== C# === | === C# === | ||
− | <syntaxhighlight lang="csharp" line> | + | <syntaxhighlight lang="csharp" line highlight="21"> |
+ | class rxBackstageEditBox_delegates_tests | ||
+ | { | ||
+ | private rxCustomUI _customUI; | ||
+ | |||
+ | // ctor | ||
+ | public rxBackstageEditBox_delegates_tests() | ||
+ | { | ||
+ | _customUI = rxCustomUI.create("my_cs_context"); | ||
+ | _customUI.clear(); | ||
+ | |||
+ | _customUI.backstage.tabs.add(new rxBackstageTab | ||
+ | { | ||
+ | label = "rxBackstageEditBox - DELEGATES TESTS" | ||
+ | }) | ||
+ | .firstColumn.groups.add(new rxBackstageGroup | ||
+ | { | ||
+ | label = "rxBackstageEditBox test group" | ||
+ | }) | ||
+ | .topItems.editBoxes.add(new rxBackstageEditBox | ||
+ | { | ||
+ | onChange = _customUI.make_delegate(new rxBackstageEditBoxDelegates.onChange(OnChange)) | ||
+ | }); | ||
+ | |||
+ | _customUI.refresh(); | ||
+ | |||
+ | } | ||
+ | |||
+ | // The callback stub | ||
+ | private void OnChange(IRibbonControl control, string text) | ||
+ | { | ||
+ | System.Diagnostics.Debug.WriteLine("OnChange() called..."); | ||
+ | MessageBox.Show("Text change :[" + text + "]"); | ||
+ | } | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== VB.NET === | === VB.NET === | ||
− | <syntaxhighlight lang="vb" line> | + | <syntaxhighlight lang="vb" line highlight="20"> |
+ | |||
+ | Public Sub New() | ||
+ | _customUI = rxCustomUI.create("my_vb_context") | ||
+ | With _customUI | ||
+ | .clear() | ||
+ | With .ribbon.tabs.add(New rxTab) | ||
+ | .label = "rxDropDownRegular - DELEGATES TESTS" | ||
+ | With .groups.add(New rxGroup) | ||
+ | .label = "My Group" | ||
+ | |||
+ | ' Add a new drop-down to the group | ||
+ | With .dropDowns.add(New rxDropDownRegular) | ||
+ | |||
+ | ' Add a few items and buttons to the drop-down | ||
+ | .items.add(New rxItem).label = "DummyItem1" | ||
+ | .items.add(New rxItem).label = "DummyItem2" | ||
+ | .buttons.add(New rxButtonRegular).label = "DummyButton1" | ||
+ | .buttons.add(New rxButtonRegular).label = "DummyButton2" | ||
+ | |||
+ | ' Register an 'onAction' callback | ||
+ | .onAction = _customUI.make_delegate(New rxDropDownRegularDelegates.onAction(AddressOf OnAction)) | ||
+ | End With | ||
+ | End With | ||
+ | End With | ||
+ | |||
+ | .refresh() | ||
+ | End With | ||
+ | End Sub | ||
+ | |||
+ | ' The callback stub | ||
+ | Private Sub OnAction(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef itemID As String, itemIndex As Integer) | ||
+ | System.Diagnostics.Debug.Print("OnAction() called...") | ||
+ | |||
+ | Dim sLabel As String = vbNullString | ||
+ | |||
+ | Dim item As rxItem = _customUI.ribbonXControls.getItemIfExists(itemID) | ||
+ | If Not (item Is Nothing) Then | ||
+ | sLabel = item.label | ||
+ | End If | ||
+ | |||
+ | MsgBox("Item with label=[" & IIf(Len(sLabel) > 0, sLabel, "[unknown]") & "] and index=[" & itemIndex & "] clicked.") | ||
+ | End Sub | ||
+ | |||
+ | End Class | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 20:45, 19 October 2013
Contents
Description
Creates a new rxDelegate object associated with a callback function and owned by the rxCustomUI instance.
Parameters
rxCustomUI.make_delegate(string) -> rxDelegate
Parameter Name
|
Parameter Type
|
Default Value
|
Description
|
methodName | string | [none] | The name of the target callback function |
return | rxDelegate | [none] | The newly created rxDelegate |
rxCustomUI.make_delegate(Delegate) -> rxDelegate
Parameter Name
|
Parameter Type
|
Default Value
|
Description
|
callbackDelegate | Delegate | [none] | An instance of a .NET delegate used to dispatch the callback |
return | rxDelegate | [none] | The newly created rxDelegate |
Remarks
- (VBA-specific) If the rxCustomUI object has global dispatch scope, the target function needs to be a global member of a standard VBA module
- Otherwise the target function needs to be a public method of the object used as the rxCustomUI's dispatch object.
- (.NET-specific) By using the second method override, Native .NET delegates can be used to instantiate rxDelegate objects. There is a *Delegates class for every Ribbon Commander control in namespace LogismiX.DynamicRibbonX.Core that specifies the callback signature of every callback (see Examples), e.g. class rxButtonDelegates contains all delegates of rxButton.
Examples
VBA
Global dispatch
Public Sub MakeDelegateTest()
' Create a new rxCustomUI with global dispatch scope
Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.Create("my_custom_context", , DispatchScope_global)
With myCustomUI
' Clear old state
.Clear
' Add new tab and label it 'My Tab'
With .ribbon.tabs.Add(New rxTab)
.Label = "My Tab"
' Add new group and label it 'My Group'
With .groups.Add(New rxGroup)
' Add a new button and label it 'Click Me!'
Dim myButton As rxButton
Set myButton = .Buttons.Add(New rxButton)
myButton.Label = "Click Me!"
' Add a delegate that dispatches to a function with name 'MyCallbackFunc'
myButton.OnAction = myCustomUI.make_delegate("MyCallbackFunc")
End With
End With
'Render the UI
.Refresh
End With
End Sub
' The callback stub
Public Sub MyCallbackFunc(ByVal control As DynamicRibbonX.IRibbonControl)
MsgBox "Button clicked!"
End Sub
See also: Creating our first button in VBA, Creating an rxCustomUI object with global dispatch scope in VBA
Local dispatch
'
' in class clsMyUI
'
Private m_customUI As rxCustomUI
Private Sub Class_Initialize()
' Instantiate a local dispatch rxCustomUI
Set m_customUI = rxCustomUI.Create("my_local_disp_ctxt", , DispatchScope_local)
With m_customUI
.Clear
' IMPORTANT: We need to specify a dispatch object through which callbacks are dispatched
' Here we have chosen to use the same class for the UI and the callbacks, which
' is convenient but also means we need to use the *_weakRef version of the method
' to avoid circular references.
Set .dispatchObject_weakRef = Me
With .ribbon.tabs.Add(New rxTab)
.Label = "Local Dispatch Tab"
With .groups.Add(New rxGroup)
.Label = "Local Dispatch Group"
With .Buttons.Add(New rxButton)
.Label = "Local Dispatch Button"
.OnAction = m_customUI.make_delegate("LocalDispCallback")
End With
End With
End With
.Refresh
End With
End Sub
' The callback stub
' NOTE: Notice it is a public method of the rxCustomUI's dispatch object, which happens to
' be this class here
Sub LocalDispCallback(ByVal control As DynamicRibbonX.IRibbonControl)
MsgBox "Called through local dispatch!"
End Sub
See also: Creating an rxCustomUI object with local dispatch scope in VBA
C#
class rxBackstageEditBox_delegates_tests
{
private rxCustomUI _customUI;
// ctor
public rxBackstageEditBox_delegates_tests()
{
_customUI = rxCustomUI.create("my_cs_context");
_customUI.clear();
_customUI.backstage.tabs.add(new rxBackstageTab
{
label = "rxBackstageEditBox - DELEGATES TESTS"
})
.firstColumn.groups.add(new rxBackstageGroup
{
label = "rxBackstageEditBox test group"
})
.topItems.editBoxes.add(new rxBackstageEditBox
{
onChange = _customUI.make_delegate(new rxBackstageEditBoxDelegates.onChange(OnChange))
});
_customUI.refresh();
}
// The callback stub
private void OnChange(IRibbonControl control, string text)
{
System.Diagnostics.Debug.WriteLine("OnChange() called...");
MessageBox.Show("Text change :[" + text + "]");
}
}
VB.NET
Public Sub New()
_customUI = rxCustomUI.create("my_vb_context")
With _customUI
.clear()
With .ribbon.tabs.add(New rxTab)
.label = "rxDropDownRegular - DELEGATES TESTS"
With .groups.add(New rxGroup)
.label = "My Group"
' Add a new drop-down to the group
With .dropDowns.add(New rxDropDownRegular)
' Add a few items and buttons to the drop-down
.items.add(New rxItem).label = "DummyItem1"
.items.add(New rxItem).label = "DummyItem2"
.buttons.add(New rxButtonRegular).label = "DummyButton1"
.buttons.add(New rxButtonRegular).label = "DummyButton2"
' Register an 'onAction' callback
.onAction = _customUI.make_delegate(New rxDropDownRegularDelegates.onAction(AddressOf OnAction))
End With
End With
End With
.refresh()
End With
End Sub
' The callback stub
Private Sub OnAction(control As LogismiX.Interop.DynamicRibbonX.IRibbonControl, ByRef itemID As String, itemIndex As Integer)
System.Diagnostics.Debug.Print("OnAction() called...")
Dim sLabel As String = vbNullString
Dim item As rxItem = _customUI.ribbonXControls.getItemIfExists(itemID)
If Not (item Is Nothing) Then
sLabel = item.label
End If
MsgBox("Item with label=[" & IIf(Len(sLabel) > 0, sLabel, "[unknown]") & "] and index=[" & itemIndex & "] clicked.")
End Sub
End Class