0
votes

I've used VB .Net in Visual Studio to create my first VSTO Addin for Excel. As is common, I had difficulty wrapping my head around the ribbon callbacks. I've gotten them working, but I have a question for which I can't find an answer:

Are there ribbon callback functions to directly get the state of a control..?

For instance, to compare ribbons to classes, most of the "clickable" controls trigger the onAction callback when they're clicked, and these can be thought of as Events. The multi-state controls such as the checkbox and togglebutton have the getPressed callback to set their current state, and these can be though of as the Property Set methods. Similarly, text-based controls have the getText callbacks.

But are there any equivalents to the Property Get methods..? Following the inverted nature of callbacks, it seems likely these would be callbacks beginning with set, such as setPressed or setText...but I have yet to find any.

Do they not exist, because of that inverted nature where get is actually set, and vice versa..?

2
Are we discussing Ribbon XML or VSTO's Ribbon Designer?Cindy Meister
Ummm...both? Unless I'm misunderstanding the tools I'm using, the Ribbon Designer is used to generate the XML used by the ribbon..? At least, that's the way I've been using it. I use the RD for the rough layout, export the XML, and then tweak it up with whatever extra properties are needed, such as the attributes for the various callbacks.spinjector
The difference is, working with the Ribbon Designer will do this for you - it makes working with Ribbon controls almost like working with Windows Forms controls. You work with properties and events instead of callbacks. There are no Set callbacks in Ribbon XML because there is no event to handle them - a callback needs a trigger. So the "setting" is part of the "get" (and other) callbacks.Cindy Meister
Okay, so I use the RD, and that exports ribbon.xml and ribbon.vb. Previously I said I tweak the XML, but I neglected to say I also tweak the generated VB file as well. That's where I create the subs and functions for onAction, getPressed, etc. I also added functionality to handle unrecognized control signatures as well, which has been very helpful. Does any of this sound like what you're referring to? I'm curious about what you describe of working with properties and events instead of callbacks. Could you elaborate on that, or point me to some reading so I could learn about it?spinjector
I'm going to write up an answer as a comment doesn't allow examples very well...Cindy Meister

2 Answers

1
votes

The VSTO Ribbon Designer "wraps" up Ribbon XML so that the developer can work with basic controls in a way similar to using Windows Forms controls. This means that properties (attributes in the XML) can be read and written to at run-time, rather than via callbacks. Also, the Ribbon control objects expose events rather than the developer needing to code event callbacks.

(Note that for anything more complex, it's necessary to use Ribbon XML. The Designer really only works for basic controls, added from the Toolbox.)

The documentation is here. Below is a small example to demonstrate.

For the example, a DropDown control has been added to a Ribbon group, named DropDown1. It can be found in the code file Ribbon1.Designer.cs where it is declared as

internal Microsoft.Office.Tools.Ribbon.RibbonDropDown dropDown1;

and implemented with

// dropDown1
// 
ribbonDropDownItem1.Label = "Item0";
ribbonDropDownItem2.Label = "Item1";
ribbonDropDownItem3.Label = "Item2";
ribbonDropDownItem4.Label = "Item3";
this.dropDown1.Items.Add(ribbonDropDownItem1);
this.dropDown1.Items.Add(ribbonDropDownItem2);
this.dropDown1.Items.Add(ribbonDropDownItem3);
this.dropDown1.Items.Add(ribbonDropDownItem4);
this.dropDown1.Label = "dropDown1";
this.dropDown1.Name = "dropDown1";
this.dropDown1.SelectionChanged += new System.EventHandler<Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs>(this.dropDown1_SelectionChanged);

Note that the information in this file is generated by the "Tools" and is not meant to be edited by the Developer. It can be, but there's no guarantee the "Tools" won't make changes negating any edits. But from it one can see what is executed when the Ribbon is loaded (attribute settings and loading the list) and the creation of an event (which would be a callback in Ribbon XML). All the properties and settings shown are generated using the Properties window provided in the Designer, same as for Windows Form controls.

Then a button was added to a Ribbon group (any tab, any group) in the project. Double-click to open the code page in Ribbon1.cs with the default Click handler and the a code stub was added. The code between the braces is typed by the developer.

Note how this code uses properties, which are read (this.dropDown1.SelectedItem.Label) and set (ddItem.Label). The code shows a message with the caption of the currently selected item, creates a new item, setting the caption and a screen tip, adds it to the dropdown, then selects that item.

private void btnSetGetDD1_Click(object sender, RibbonControlEventArgs e)
{
    MessageBox.Show(this.dropDown1.SelectedItem.Label);
    RibbonDropDownItem ddItem = new RibbonDropDownItem();
    ddItem.Label = "new item three added";
    ddItem.ScreenTip = "next test";
    this.dropDown1.Items.Add(ddItem);
    this.dropDown1.SelectedItemIndex = this.dropDown1.Items.Count - 1;
}
1
votes

There are no callbacks to get the values of these properties - since you are the one who is setting the various values, you should be the one storing the values internally and thus exposing them both for the ribbon and for your code.