Difference between revisions of "Using delegates in CS"
From Ribbon Commander Documentation
(→Example) |
|||
Line 146: | Line 146: | ||
* Run the add-in. If everything went according to plan, the following UI will be created: | * Run the add-in. If everything went according to plan, the following UI will be created: | ||
*: [[image:UsingDelegatesInCSCheckbox.png|link=]] | *: [[image:UsingDelegatesInCSCheckbox.png|link=]] | ||
+ | |||
+ | == Remarks == | ||
+ | See [[Inserting Delegate Stubs in VS2012 (CS)|Inserting Delegate Stubs in VS2012 (C#)]] for instructions on how to leverage Visual Studio 2010 to insert delegate stubs automatically. |
Revision as of 16:45, 15 March 2013
Contents
Prerequisites
We recommend you go through A 'hello world' C# program before going into this example.
Background
You can subscribe to events of Dynamic RibbonX controls by using .NET events or .NET delegates. Here, we show how it can be done by using .NET delegates.
- Every Dynamic RibbonX control has an associated *Delegates class in namespace LogismiX.DynamicRibbonX.Core which exposes all delegate signatures used by the control for its callbacks.
- Visual Studio 2012 allows you to automatically insert delegate stubs based on their signature instead of typing the code (and double-checking their signature) manually. For earlier versions of Visual Studio, we recommend you use events whose stubs can be inserted automatically instead, where possible.
Example
In this example we will set up a UI that contains an rxEditBox control and subscribe to all its delegates.
- Add a new class to your project and name it EditBoxDelegatesUI
- Add the following code to your class
class EditBoxDelegatesUI
{
private rxCustomUI _customUI;
private rxEditBox _myEdit;
public EditBoxDelegatesUI()
{
// Instantiate a new rxCustomUI in a new context with id 'my_test_cs_ctx'
_customUI = rxCustomUI.create("my_test_cs_ctx", "Test C# Context");
_customUI.clear();
// Add a new tab and label it
rxTab myTab = _customUI.ribbon.tabs.add(new rxTab());
myTab.label = "My C# Tab";
// Add a new group to the tab and label it
rxGroup myGroup = myTab.groups.add(new rxGroup());
myGroup.label = "My C# Group";
// Add a new checkbox to the group and subscribe to all its events
_myEdit = myGroup.editBoxes.add(new rxEditBox
{
onChange = _customUI.make_delegate(new rxEditBoxDelegates.onChange(OnChangeCallback)),
getEnabled = _customUI.make_delegate(new rxEditBoxDelegates.getEnabled(GetEnabledCallback)),
getKeytip = _customUI.make_delegate(new rxEditBoxDelegates.getKeytip(GetKeytipCallback)),
getImage = _customUI.make_delegate(new rxEditBoxDelegates.getImage(GetImageCallback)),
getLabel = _customUI.make_delegate(new rxEditBoxDelegates.getLabel(GetLabelCallback)),
getScreentip = _customUI.make_delegate(new rxEditBoxDelegates.getScreentip(GetScreentipCallback)),
getShowImage = _customUI.make_delegate(new rxEditBoxDelegates.getShowImage(GetShowImageCallback)),
getShowLabel = _customUI.make_delegate(new rxEditBoxDelegates.getShowLabel(GetShowLabelCallback)),
getSupertip = _customUI.make_delegate(new rxEditBoxDelegates.getSupertip(GetSupertipCallback)),
getText = _customUI.make_delegate(new rxEditBoxDelegates.getText(GetTextCallback)),
getVisible = _customUI.make_delegate(new rxEditBoxDelegates.getVisible(GetVisibleCallback))
});
// Render the UI
_customUI.refresh();
}
private bool GetVisibleCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetVisibleCallback called...");
return true;
}
private string GetTextCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetTextCallback called...");
return "Initial Text";
}
private string GetSupertipCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetSupertipCallback called...");
return "My CheckBox supertip";
}
private bool GetShowLabelCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetShowLabelCallback called...");
return true;
}
private bool GetShowImageCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetShowImageCallback called...");
return true;
}
private string GetScreentipCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetScreentipCallback called...");
return "My Checkbox screentip";
}
private string GetLabelCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetLabelCallback called...");
return "My C# CheckBox";
}
private object GetImageCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetImageCallback called...");
return "Chart3DColumnChart";
}
private string GetKeytipCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetKeytipCallback called...");
return "K";
}
private bool GetEnabledCallback(IRibbonControl control)
{
System.Diagnostics.Debug.Print("GetEnabledCallback called...");
return true;
}
private void OnChangeCallback(IRibbonControl control, string text)
{
System.Diagnostics.Debug.Print("OnChangeCallback called...");
MessageBox.Show("CheckBox text changed. New text: [" + text + "]");
}
}
- Modify class ThisAddin in ThisAddin.cs as follows
namespace DynamicRibbonXAddin
{
public partial class ThisAddIn
{
private EditBoxDelegatesUI _myCustomUI;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Show the custom ribbon
_myCustomUI2 = new EditBoxDelegatesUI();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
- Run the add-in. If everything went according to plan, the following UI will be created:
Remarks
See Inserting Delegate Stubs in VS2012 (C#) for instructions on how to leverage Visual Studio 2010 to insert delegate stubs automatically.