Windows Ribbon for WinForms, Part 19 – RecentItems

November 30, 2009


Windows Ribbon for WinForms library now supports working with recent items in the application menu.
The result of this post is a yet another sample, “16-RecentItems”, found on the project site.


What are recent items?
Recent items are items in a list which appears in the application menu. They doesn’t have to be file names and they doesn’t have to be recent, although it is recommended.

Every item has 3 properties:

  • Label – Item name, usually file name without path

  • Label Description – Item tooltip, usually full filename path

  • Pinned – Boolean that indicates whether the recent item should not be removed from the list

More details can be found at Recent Items on MSDN.

Using RecentItems – Ribbon Markup
Commands section:

  <Command Name=cmdRecentItems Id=1005 LabelTitle=Recent Items />

Views section:

      <ApplicationMenu CommandName=cmdApplicationMenu>
          <RecentItems CommandName=cmdRecentItems EnablePinning=true MaxCount=7 />

Things to note:

  • The “Recent Items” label can be changed to whatever you need (e.g. “Days of the week”).

  • Setting EnablePinning attribute to false will hide the pins from the application menu.

  • MaxCount attribute specify how many items to display on the application menu.

Using RecentItems – Code Behind

private Ribbon _ribbon;
private RibbonRecentItems _ribbonRecentItems;

List<RecentItemsPropertySet> _recentItems;

public Form1()

    _ribbon = new Ribbon();
    _ribbonRecentItems = new RibbonRecentItems(_ribbon, (uint)RibbonMarkupCommands.cmdRecentItems);

    _ribbonRecentItems.OnExecute += new OnExecuteEventHandler(_recentItems_OnExecute);

private void Form1_Load(object sender, EventArgs e)


private void InitRecentItems()
    // prepare list of recent items
    _recentItems = new List<RecentItemsPropertySet>();
    _recentItems.Add(new RecentItemsPropertySet()
                         Label = “Recent item 1”,
                         LabelDescription = “Recent item 1 description”,
                         Pinned = true
    _recentItems.Add(new RecentItemsPropertySet()
                         Label = “Recent item 2”,
                         LabelDescription = “Recent item 2 description”,
                         Pinned = false

    _ribbonRecentItems.RecentItems = _recentItems;

RibbonRecentItems is the helper class for working with the recent items feature. It has a property named RecentItems of type IList<RecentItemsPropertySet>. This property contains the list of the recent items. Note that it is the user responsibility for providing this list and update it when needed (add / remove items, change pinned state).

Responding to a click on an item:

void _recentItems_OnExecute(PropertyKeyRef key, PropVariantRef currentValue, IUISimplePropertySet commandExecutionProperties)
    if (key.PropertyKey == RibbonProperties.RecentItems)
        // go over recent items
        object[] objectArray = (object[])currentValue.PropVariant.Value;
        for (int i = 0; i < objectArray.Length; ++i)
            IUISimplePropertySet propertySet = objectArray[i] as IUISimplePropertySet;

            if (propertySet != null)
                PropVariant propLabel;
                propertySet.GetValue(ref RibbonProperties.Label,
                                     out propLabel);
                string label = (string)propLabel.Value;

                PropVariant propLabelDescription;
                propertySet.GetValue(ref RibbonProperties.LabelDescription,
                                     out propLabelDescription);
                string labelDescription = (string)propLabelDescription.Value;

                PropVariant propPinned;
                propertySet.GetValue(ref RibbonProperties.Pinned,
                                     out propPinned);
                bool pinned = (bool)propPinned.Value;

                // update pinned value
                _recentItems[i].Pinned = pinned;
    else if (key.PropertyKey == RibbonProperties.SelectedItem)
        // get selected item index
        uint selectedItem = (uint)currentValue.PropVariant.Value;

        // get selected item label
        PropVariant propLabel;
        commandExecutionProperties.GetValue(ref RibbonProperties.Label,
                                            out propLabel);
        string label = (string)propLabel.Value;

        // get selected item label description
        PropVariant propLabelDescription;
        commandExecutionProperties.GetValue(ref RibbonProperties.LabelDescription,
                                            out propLabelDescription);
        string labelDescription = (string)propLabelDescription.Value;

        // get selected item pinned value
        PropVariant propPinned;
        commandExecutionProperties.GetValue(ref RibbonProperties.Pinned,
                                            out propPinned);
        bool pinned = (bool)propPinned.Value;

I know, some explanations are in order.
The OnExecute event is called on two occasions:

  1. When the user clicks on one of the items.

  2. When the user changes the pinned status of several items and then closes the menu (either by selecting one of the items or by clicking outside the menu).

When the user clicks on an item, the currentValue argument contains the index of the selected item and commandExecutionProperties argument contains the properties of the selected item. The above code shows how to extract them.

When the user changes the pinned status of several items, the currentValue argument contains the new status of the items. It is the user responsibility to update the items in its own list. otherwise the user’s change won’t appear the next time he opens the menu.

That’s it for now,
Arik Poznanski.

kick it on Shout it

Add comment
facebook linkedin twitter email

Leave a Reply


  1. KyleDecember 1, 2009 ב 08:25

    Since I’m lazy and like visual designers, I suppose it’s possible to use the WPF ribbon designer and use the xml file with your ribbon control (only using controls that are supported by your WinForm version)?

  2. arikDecember 3, 2009 ב 22:08

    Unfortunately, the ribbon markup syntax varies between the different ribbon frameworks, so no chance of mixing ribbon designers.

  3. Michiel PeetersFebruary 25, 2010 ב 22:15

    Is it also possible to disable the recent items and that you can only use the application menu?

  4. Michiel PeetersFebruary 25, 2010 ב 22:42

    Hey, is it possible to remove the recent items list in the application menu?

  5. arikFebruary 26, 2010 ב 00:06

    If you don’t want the recent items list, just don’t declare it in the ribbon xml.

    Parts 4 & 5 shows examples of application menu without recent items list.
    See table of contents here: