Difference between revisions of "Using delegates in CS"

From Ribbon Commander Documentation
Jump to: navigation, search
m (Rxdff15551 bb53 moved page Using delegates in C to Using delegates in CS)
(Background)
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Under Construction}}
+
{{DISPLAYTITLE:Using delegates in C#}}
 +
== Prerequisites ==
 +
We recommend you go through [[A 'hello world' CS program|A 'hello world' C# program]] before going into this example.
 +
 
 +
== Background ==
 +
You can subscribe to events of [[controls|Ribbon Commander controls]] by using [[Using events in CS|.NET events]] or .NET delegates. Here, we show how it can be done by using .NET delegates.
 +
 
 +
* Every [[controls|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 [[Using events in CS|events]] whose stubs can be [[Inserting Events Stubs in CS|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 [[rxEditBox#Delegates|delegates]].
 +
* Add a new class to [[Creating a new project in CSharp|your project]] and name it EditBoxDelegatesUI
 +
* Add the following code to your class
 +
<syntaxhighlight lang="csharp" line>
 +
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 + "]");
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
* Modify class ThisAddin in ThisAddin.cs as follows
 +
<syntaxhighlight lang="csharp" line>
 +
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)
 +
        {
 +
        }
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 
 +
* Run the add-in. If everything went according to plan, the following UI will be created:
 +
*: [[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.

Latest revision as of 20:51, 19 October 2013

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.