The OnAction checkbox does nothing

I have a custom feed with a few checkboxes in the menu:

<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load"> <ribbon> <tabs> <tab id="ribbon" label="Ribbon"> <group id="ribbonGroup" label="Group"> <menu id="menu" label="Menu"> <checkBox id="checkbox1" label="Checkbox 1" visible="true" onAction="OnCheckboxChanged"/> <checkBox id="checkbox2" label="Checkbox 2" visible="true" onAction="OnCheckboxChanged"/> <checkBox id="checkbox2" label="Checkbox 2" visible="true" onAction="OnCheckboxChanged"/> </group> </tab> </tabs> </ribbon> </customUI> 

This is the corresponding C # code:

 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Drawing; using System.Windows.Forms; using Office = Microsoft.Office.Core; using Excel = Microsoft.Office.Interop.Excel; using Microsoft.Office.Tools.Excel; namespace ExcelAddIn1 { [ComVisible(true)] public class SSRRibbon : Office.IRibbonExtensibility { private Office.IRibbonUI ribbon; public SSRRibbon() { } #region IRibbonExtensibility-Member public string GetCustomUI(string ribbonID) { return GetResourceText("ExcelAddIn1.SSRRibbon.xml"); } #endregion #region ribbon callback functions public void Ribbon_Load(Office.IRibbonUI ribbonUI) { this.ribbon = ribbonUI; } public void OnCheckboxChanged(Office.IRibbonControl control) { int i = 1; } #endregion #region auxiliary private static string GetResourceText(string resourceName) { Assembly asm = Assembly.GetExecutingAssembly(); string[] resourceNames = asm.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; ++i) { if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0) { using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i]))) { if (resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #endregion } } 

However, OnCheckboxChanged never called. It works great when I use this callback function with buttons, but not with checkboxes, either in the menu or directly in the feed group. It also works with getPressed instead of onAction .

+5
source share
1 answer

This thread and this MSDN Social thread show that onAction has a second argument in the parameter; isPressed .

 public void markAsRead_OnAction(Office.IRibbonControl control, bool isPressed) { public void cbViewAction(Office.IRibbonControl control, bool pressed) { 

It also means official documentation for this :

 public void OnActionCallback(Office.IRibbonControl control, bool isPressed) { if (control.Id == "checkBox1") { MessageBox.Show("You clicked " + control.Id); } else { MessageBox.Show("You clicked a different control."); } } 

Defining callback methods

  • It must be declared public.
  • Its name must match the name of the callback method that you assigned to the control in the feed XML file.
  • Its signature must match the signature of the type of callback method available for the associated ribbon control.

Following this article, I read Configuring 2007 Fluent Ribbon for Developers (Part 3 of 3)

Can I have two callbacks with the same name but with different signatures?

Although you can do this, we recommended that you have different callbacks for each control (and do not consider the built-in overload to handle the differences between the two callbacks). For example, suppose you write Fluent UI with two callbacks with the same name as in the following code.

 public void doSomething(IRibbonControl control, bool pressState); public void doSomething(IRibbonControl control); 

Also suppose your XML markup defines a toggleButton control and a button control, and each of them has a callback onAction = "doSomething". In this case, only the toggleButton control will work due to the automatic generation of IDispatch Visual Basic and Visual C #. If you write a C ++ add-in and implement IDispatch yourself, this case will work. (In other words, it’s better not to do this.)

0
source

Source: https://habr.com/ru/post/1269482/


All Articles