15

So I have a dropdown menu in a ribbon with contents that can be changed while it is being used. Outlook is also happy to let me 'add' or 'insert' items into it, as long as I do not add more than 1 item.

If I try to, I'll be told that the index is out of bounds rather than expanding the upper bounds for me.

I find that if I insert it into the collection in the designer portion of the code, it will work fine, but designer code is only run once, unless I Dispose the ribbon and re-create it.

Any ideas regarding how I can get this working

CodeMinion
  • 613
  • 2
  • 10
  • 24

5 Answers5

49

Try this. This should work for you.

RibbonDropDownItem item 
      = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
item.Label = "First Name";
this.cbRecent.Items.Add(item);
sadanand sudeer
  • 501
  • 4
  • 4
  • Hello.... if the dropdown contains a button, items are added before the button.... any way to add the items after the buttons? – jstuardo Nov 14 '17 at 17:57
  • It is worth nothing that the `this.cbRecent.ShowItemLabel = true;` would need to be set, and somehow wasn't be default in my setup. Obviosuly `cbRecent` here is the ComboBox – James Robinson Dec 10 '18 at 20:09
  • what if we want want key-value pair? we seem to have only "label" available for that? – T.Todua Feb 17 '19 at 16:00
12

Try the following directly inside the Ribbon Class:

RibbonDropDownItem item = this.Factory.CreateRibbonDropDownItem();
item.Label = "Text";
combo.Items.Add(item);
Ama
  • 903
  • 5
  • 19
Lee
  • 121
  • 1
  • 2
  • 2
    The answer seems good, but can you provide a bit of description? It would be very useful! – Daniele B Nov 21 '12 at 14:37
  • what if we want want key-value pair? we seem to have only "label" available for that? – T.Todua Feb 17 '19 at 16:00
  • A bit late but if you want to store data and refer to it via a "key", you can use the `.Label` property as your key, and the `.Tag` property as your value (can be a data variable or even an object) – Ama Jul 01 '19 at 15:49
2

jeds, your approach doesn't work with "new". You have to use the "Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem()". Otherwise, you are right and your approach works great with a RibbonGallery.

That approach also works great with a DropDown. I'm still often conflicted about which one to use...

However, other than those 2 objects (Dropdown and RibbonGallery), I believe drventure is correct. You simply have to stub out the objects ahead of time and use them as needed.

You can also use the XML Ribbon, but that creates an even bigger set of headaches (at least for my use cases).

Brad
  • 51
  • 3
0

Try using a Ribbon Gallery. I have been able to modify them during run-time with as little as

foreach (string s in list)
{
     RibbonDropDownItem item = new RibbonDropDownItem();                
     item.Label = s;
     rGallery.Items.Add(item);                
}

where rGallery is a RibbonGallery.

  • I don't see a necessity to substitute a RibbonGallery for the DropDown - your method works just as fine with a DropDown. See sadanand sudeer's answer. – Aaron Thoma Mar 04 '12 at 16:23
-2

Generally speaking, VSTO wants you to completely describe the UI elements you need one time, the very first time you're asked for them (via GetCustomUI).

I've run into similar probs before with vsto and about the only reasonable way around it I've found was to prepopulate (via the designer) all the elements you might need (so let's say 10 items in your drop down list).

Then, programmatically HIDE or SHOW those items and update their captions and other properties as necessary while your addin runs.

That way, you never have to dynamically add or remove anything.

DarinH
  • 4,768
  • 2
  • 20
  • 32
  • No prob. It's a little weird and hackish, and it can make a certain class of features a bit difficult, but as far as I know, that's about the only choice you have. – DarinH Apr 21 '11 at 15:38
  • 5
    If I'm not mistaken, this answer is plain false. See the other answers. – Aaron Thoma Mar 04 '12 at 16:21
  • Possibly, but the two examples given don't explicitly mention performing that code anywhere past the initial load of the ribbon ui. What I'd run into was problems attempting to modify UI elements once GetCustomUI and all of the initialization functions had been performed. Still the other answers are definitely worth looking into. But I don't think my answer was "wrong" per se, just an alternative (and guaranteed functional) way to do what the op was asking without a lot of headache. – DarinH Mar 05 '12 at 17:10
  • Nope, this is wrong, plain and simple. The highest voted answer is the correct solution as far as I can see. – Richard Hansell Sep 06 '13 at 13:58