Using delegates in CS

From Ribbon Commander Documentation
Jump to: navigation, search

Prerequisites

We recommend you go through A 'hello world' C# program before going into this example.

Background

You can subscribe to events of Ribbon Commander controls by using .NET events or .NET delegates. Here, we show how it can be done by using .NET delegates.

  • Every Ribbon Commander 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
  1. class EditBoxDelegatesUI
  2. {
  3.     private rxCustomUI _customUI;
  4.     private rxEditBox _myEdit;
  5.  
  6.     public EditBoxDelegatesUI()
  7.     {
  8.         // Instantiate a new rxCustomUI in a new context with id 'my_test_cs_ctx'
  9.         _customUI = rxCustomUI.create("my_test_cs_ctx", "Test C# Context");
  10.  
  11.         _customUI.clear();
  12.  
  13.         // Add a new tab and label it
  14.         rxTab myTab = _customUI.ribbon.tabs.add(new rxTab());
  15.         myTab.label = "My C# Tab";
  16.  
  17.         // Add a new group to the tab and label it
  18.         rxGroup myGroup = myTab.groups.add(new rxGroup());
  19.         myGroup.label = "My C# Group";
  20.  
  21.         // Add a new checkbox to the group and subscribe to all its events
  22.         _myEdit = myGroup.editBoxes.add(new rxEditBox
  23.         {                
  24.             onChange = _customUI.make_delegate(new rxEditBoxDelegates.onChange(OnChangeCallback)),
  25.             getEnabled = _customUI.make_delegate(new rxEditBoxDelegates.getEnabled(GetEnabledCallback)),
  26.             getKeytip = _customUI.make_delegate(new rxEditBoxDelegates.getKeytip(GetKeytipCallback)),
  27.             getImage = _customUI.make_delegate(new rxEditBoxDelegates.getImage(GetImageCallback)),
  28.             getLabel = _customUI.make_delegate(new rxEditBoxDelegates.getLabel(GetLabelCallback)),
  29.             getScreentip = _customUI.make_delegate(new rxEditBoxDelegates.getScreentip(GetScreentipCallback)),
  30.             getShowImage = _customUI.make_delegate(new rxEditBoxDelegates.getShowImage(GetShowImageCallback)),
  31.             getShowLabel = _customUI.make_delegate(new rxEditBoxDelegates.getShowLabel(GetShowLabelCallback)),
  32.             getSupertip = _customUI.make_delegate(new rxEditBoxDelegates.getSupertip(GetSupertipCallback)),
  33.             getText = _customUI.make_delegate(new rxEditBoxDelegates.getText(GetTextCallback)),
  34.             getVisible = _customUI.make_delegate(new rxEditBoxDelegates.getVisible(GetVisibleCallback))            
  35.         });
  36.  
  37.         // Render the UI
  38.         _customUI.refresh();
  39.  
  40.     }
  41.  
  42.     private bool GetVisibleCallback(IRibbonControl control)
  43.     {
  44.         System.Diagnostics.Debug.Print("GetVisibleCallback called...");
  45.         return true;
  46.     }
  47.  
  48.     private string GetTextCallback(IRibbonControl control)
  49.     {
  50.         System.Diagnostics.Debug.Print("GetTextCallback called...");
  51.         return "Initial Text";
  52.     }
  53.  
  54.     private string GetSupertipCallback(IRibbonControl control)
  55.     {
  56.         System.Diagnostics.Debug.Print("GetSupertipCallback called...");
  57.         return "My CheckBox supertip";
  58.     }
  59.  
  60.     private bool GetShowLabelCallback(IRibbonControl control)
  61.     {
  62.         System.Diagnostics.Debug.Print("GetShowLabelCallback called...");
  63.         return true;
  64.     }
  65.  
  66.     private bool GetShowImageCallback(IRibbonControl control)
  67.     {
  68.         System.Diagnostics.Debug.Print("GetShowImageCallback called...");
  69.         return true;
  70.     }
  71.  
  72.     private string GetScreentipCallback(IRibbonControl control)
  73.     {
  74.         System.Diagnostics.Debug.Print("GetScreentipCallback called...");
  75.         return "My Checkbox screentip";
  76.     }
  77.  
  78.     private string GetLabelCallback(IRibbonControl control)
  79.     {
  80.         System.Diagnostics.Debug.Print("GetLabelCallback called...");
  81.         return "My C# CheckBox";
  82.     }
  83.  
  84.     private object GetImageCallback(IRibbonControl control)
  85.     {
  86.         System.Diagnostics.Debug.Print("GetImageCallback called...");
  87.         return "Chart3DColumnChart";
  88.     }
  89.  
  90.     private string GetKeytipCallback(IRibbonControl control)
  91.     {
  92.         System.Diagnostics.Debug.Print("GetKeytipCallback called...");
  93.         return "K";
  94.     }
  95.  
  96.     private bool GetEnabledCallback(IRibbonControl control)
  97.     {
  98.         System.Diagnostics.Debug.Print("GetEnabledCallback called...");
  99.         return true;
  100.     }
  101.  
  102.     private void OnChangeCallback(IRibbonControl control, string text)
  103.     {
  104.         System.Diagnostics.Debug.Print("OnChangeCallback called...");
  105.         MessageBox.Show("CheckBox text changed. New text: [" + text + "]");
  106.     }
  107. }
  • Modify class ThisAddin in ThisAddin.cs as follows
  1. namespace DynamicRibbonXAddin
  2. {
  3.     public partial class ThisAddIn
  4.     {
  5.         private EditBoxDelegatesUI _myCustomUI;
  6.  
  7.         private void ThisAddIn_Startup(object sender, System.EventArgs e)
  8.         {
  9.             // Show the custom ribbon
  10.             _myCustomUI2 = new EditBoxDelegatesUI();
  11.         }
  12.  
  13.         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
  14.         {
  15.         }
  16.     }
  17. }
  • Run the add-in. If everything went according to plan, the following UI will be created:
    UsingDelegatesInCSCheckbox.png

Remarks

See Inserting Delegate Stubs in VS2012 (C#) for instructions on how to leverage Visual Studio 2010 to insert delegate stubs automatically.