Difference between revisions of "Using events in CS"
From Ribbon Commander Documentation
(→Background) |
(→Example) |
||
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | {{DISPLAYTITLE: | + | {{DISPLAYTITLE:Using Events in C#}} |
== Prerequisites == | == Prerequisites == | ||
− | We recommend you go through [[ | + | We recommend you go through [[A 'hello world' CS program|A 'hello world' C# program]] before going into this example. |
== Background == | == Background == | ||
− | You can subscribe to events of [[controls| | + | You can subscribe to events of [[controls|Ribbon Commander controls]] by using .NET events or [[Using_delegates_in_CS|.NET delegates]]. Here, we show how it can be done by using .NET events. |
In general you need to take the following steps: | In general you need to take the following steps: | ||
* Declare a member variable of the events source control type (e.g. [[rxCheckBox]]) | * Declare a member variable of the events source control type (e.g. [[rxCheckBox]]) | ||
* Assign the member variable to an object of the type (e.g. instantiate a new variable) | * Assign the member variable to an object of the type (e.g. instantiate a new variable) | ||
− | * Use the standard operator += to add | + | * Use the ''standard operator +='' to add event handlers to events you are interested need to subscribe to. Intellisense allows you to insert event stubs automatically. |
+ | |||
+ | == Example == | ||
+ | In this example we will set up a UI that contains an [[rxCheckBox]] control and subscribe to all its [[rxCheckBox#Events|events]]. | ||
+ | * Add a new class to [[Creating a new project in CSharp|your project]] and name it CheckBoxEventsUI | ||
+ | * Add the following code to your class | ||
+ | <syntaxhighlight lang="csharp" line> | ||
+ | // Namespaces of the two Ribbon Commander assemblies | ||
+ | using IlydaUK.Interop.RibbonCommander; | ||
+ | using IlydaUK.RibbonCommander.Core; | ||
+ | |||
+ | namespace RibbonCommanderAddin | ||
+ | { | ||
+ | class CheckBoxEventsUI | ||
+ | { | ||
+ | private rxCustomUI _customUI; | ||
+ | private rxCheckBox _myCheckBox; | ||
+ | |||
+ | public CheckBoxEventsUI() | ||
+ | { | ||
+ | // 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 | ||
+ | _myCheckBox = myGroup.checkBoxes.add(new rxCheckBox()); | ||
+ | _myCheckBox.OnActionEvent += _myCheckBox_OnActionEvent; | ||
+ | _myCheckBox.OnGetDescription += _myCheckBox_OnGetDescription; | ||
+ | _myCheckBox.OnGetEnabled += _myCheckBox_OnGetEnabled; | ||
+ | _myCheckBox.OnGetKeytip += _myCheckBox_OnGetKeytip; | ||
+ | _myCheckBox.OnGetLabel += _myCheckBox_OnGetLabel; | ||
+ | _myCheckBox.OnGetPressed += _myCheckBox_OnGetPressed; | ||
+ | _myCheckBox.OnGetScreentip += _myCheckBox_OnGetScreentip; | ||
+ | _myCheckBox.OnGetSupertip += _myCheckBox_OnGetSupertip; | ||
+ | _myCheckBox.OnGetVisible += _myCheckBox_OnGetVisible; | ||
+ | |||
+ | // Render the UI | ||
+ | _customUI.refresh(); | ||
+ | |||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetVisible(IRibbonControl control, out bool visible) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetVisible called..."); | ||
+ | |||
+ | visible = true; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetSupertip(IRibbonControl control, out string supertip) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetSupertip called..."); | ||
+ | |||
+ | supertip = "My CheckBox Supertip"; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetScreentip(IRibbonControl control, out string screentip) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetScreentip called..."); | ||
+ | |||
+ | screentip = "My CheckBox Screentip"; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetPressed(IRibbonControl control, out bool pressed) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetPressed called..."); | ||
+ | // The checkbox will not be ticked to begin with | ||
+ | pressed = false; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetLabel(IRibbonControl control, out string label) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetLabel called..."); | ||
+ | |||
+ | label = "My CheckBox"; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetKeytip(IRibbonControl control, out string keytip) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetKeytip called..."); | ||
+ | |||
+ | |||
+ | keytip = "K"; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetEnabled(IRibbonControl control, out bool enabled) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetEnabled called..."); | ||
+ | |||
+ | enabled = true; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnGetDescription(IRibbonControl control, out string description) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnGetDescription called..."); | ||
+ | |||
+ | description = "My CheckBox description"; | ||
+ | } | ||
+ | |||
+ | void _myCheckBox_OnActionEvent(IRibbonControl control, ref bool pressed) | ||
+ | { | ||
+ | System.Diagnostics.Debug.Print("_myCheckBox_OnActionEvent called..."); | ||
+ | |||
+ | MessageBox.Show("Checkbox " + (pressed ? "clicked" : "un-clicked")); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | * Modify class ThisAddin in ThisAddin.cs as follows | ||
+ | <syntaxhighlight lang="csharp" line> | ||
+ | namespace RibbonCommanderAddin | ||
+ | { | ||
+ | public partial class ThisAddIn | ||
+ | { | ||
+ | private CheckBoxEventsUI _myCustomUI; | ||
+ | |||
+ | private void ThisAddIn_Startup(object sender, System.EventArgs e) | ||
+ | { | ||
+ | // Show the custom ribbon | ||
+ | _myCustomUI2 = new CheckBoxEventsUI(); | ||
+ | } | ||
+ | |||
+ | private void ThisAddIn_Shutdown(object sender, System.EventArgs e) | ||
+ | { | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Remarks == | ||
+ | See [[Inserting Events Stubs in CS|Inserting Events Stubs in C#]] for instructions on how to leverage Visual Studio to insert event stubs automatically. |
Latest revision as of 10:35, 21 October 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 Ribbon Commander controls by using .NET events or .NET delegates. Here, we show how it can be done by using .NET events. In general you need to take the following steps:
- Declare a member variable of the events source control type (e.g. rxCheckBox)
- Assign the member variable to an object of the type (e.g. instantiate a new variable)
- Use the standard operator += to add event handlers to events you are interested need to subscribe to. Intellisense allows you to insert event stubs automatically.
Example
In this example we will set up a UI that contains an rxCheckBox control and subscribe to all its events.
- Add a new class to your project and name it CheckBoxEventsUI
- Add the following code to your class
// Namespaces of the two Ribbon Commander assemblies
using IlydaUK.Interop.RibbonCommander;
using IlydaUK.RibbonCommander.Core;
namespace RibbonCommanderAddin
{
class CheckBoxEventsUI
{
private rxCustomUI _customUI;
private rxCheckBox _myCheckBox;
public CheckBoxEventsUI()
{
// 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
_myCheckBox = myGroup.checkBoxes.add(new rxCheckBox());
_myCheckBox.OnActionEvent += _myCheckBox_OnActionEvent;
_myCheckBox.OnGetDescription += _myCheckBox_OnGetDescription;
_myCheckBox.OnGetEnabled += _myCheckBox_OnGetEnabled;
_myCheckBox.OnGetKeytip += _myCheckBox_OnGetKeytip;
_myCheckBox.OnGetLabel += _myCheckBox_OnGetLabel;
_myCheckBox.OnGetPressed += _myCheckBox_OnGetPressed;
_myCheckBox.OnGetScreentip += _myCheckBox_OnGetScreentip;
_myCheckBox.OnGetSupertip += _myCheckBox_OnGetSupertip;
_myCheckBox.OnGetVisible += _myCheckBox_OnGetVisible;
// Render the UI
_customUI.refresh();
}
void _myCheckBox_OnGetVisible(IRibbonControl control, out bool visible)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetVisible called...");
visible = true;
}
void _myCheckBox_OnGetSupertip(IRibbonControl control, out string supertip)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetSupertip called...");
supertip = "My CheckBox Supertip";
}
void _myCheckBox_OnGetScreentip(IRibbonControl control, out string screentip)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetScreentip called...");
screentip = "My CheckBox Screentip";
}
void _myCheckBox_OnGetPressed(IRibbonControl control, out bool pressed)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetPressed called...");
// The checkbox will not be ticked to begin with
pressed = false;
}
void _myCheckBox_OnGetLabel(IRibbonControl control, out string label)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetLabel called...");
label = "My CheckBox";
}
void _myCheckBox_OnGetKeytip(IRibbonControl control, out string keytip)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetKeytip called...");
keytip = "K";
}
void _myCheckBox_OnGetEnabled(IRibbonControl control, out bool enabled)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetEnabled called...");
enabled = true;
}
void _myCheckBox_OnGetDescription(IRibbonControl control, out string description)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnGetDescription called...");
description = "My CheckBox description";
}
void _myCheckBox_OnActionEvent(IRibbonControl control, ref bool pressed)
{
System.Diagnostics.Debug.Print("_myCheckBox_OnActionEvent called...");
MessageBox.Show("Checkbox " + (pressed ? "clicked" : "un-clicked"));
}
}
}
- Modify class ThisAddin in ThisAddin.cs as follows
namespace RibbonCommanderAddin
{
public partial class ThisAddIn
{
private CheckBoxEventsUI _myCustomUI;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Show the custom ribbon
_myCustomUI2 = new CheckBoxEventsUI();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
Remarks
See Inserting Events Stubs in C# for instructions on how to leverage Visual Studio to insert event stubs automatically.