RecentItems - harborsiem/WinForms-Ribbon GitHub Wiki

RecentItems

Support for 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.

RecentItems

What are recent items? Recent items are items in a list which appears in the application menu. They don’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

  • LabelDescription – 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:

<Application.Commands>
 …
 <Command Name=“cmdRecentItems“ Id=“1005“ LabelTitle=“Recent Items“ />
</Application.Commands>

Views section:

<Application.Views>
 <Ribbon>
   <Ribbon.ApplicationMenu>
     <ApplicationMenu CommandName=“cmdApplicationMenu“>
       <ApplicationMenu.RecentItems>
         <RecentItems CommandName=“cmdRecentItems“ EnablePinning=“true“ MaxCount=“7“ />
       </ApplicationMenu.RecentItems>
       …
     </ApplicationMenu>
   </Ribbon.ApplicationMenu>
 </Ribbon>
</Application.Views>

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 Initialization:

partial class RibbonItems
{
    private IList<RecentItemsPropertySet> _recentItems;

    public void Init()
    {
        RecentItems.SelectedChanged += new EventHandler<SelectedRecentEventArgs>(_recentItems_ExecuteEvent);
        RecentItems.PinnedChanged += RecentItems_PinnedChanged;
    }
}

public void Load()
{
    InitRecentItems();
}

private void InitRecentItems()
{
    // prepare list of recent items
    _recentItems = RecentItems.RecentItems;
    _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 is the helper class for working with the recent items feature. It has a property named RecentItems of type IList. 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_ExecuteEvent(object sender, SelectedRecentEventArgs e)
{
    int selectedItem = e.SelectedItem.SelectedItemIndex;
    RecentItemsPropertySet propertySet = e.SelectedItem.PropertySet;
    string label = propertySet.Label;
    string labelDescription = propertySet.LabelDescription;
    bool pinned = propertySet.Pinned;
    int maxCount = RecentItems.MaxCount;
    MessageBox.Show("Selected Recent index: " + e.SelectedItem.SelectedItemIndex + Environment.NewLine + "MaxCount: " + maxCount);
}

private void RecentItems_PinnedChanged(object sender, PinnedChangedEventArgs e)
{
   for (int i = 0; i < e.ChangedPinnedIndices.Count; ++i)
   {
       RecentItemsPropertySet propSet = RecentItems.RecentItems[i];
       string label = propSet.Label;
       string labelDescription = propSet.LabelDescription;
       bool pinned = propSet.Pinned;
   }
}

I know, some explanations are in order.

The SelectedChanged event is called: When the user clicks on one of the items. When the user clicks on an item, the e.SelectedItem argument contains the index of the selected item and the RecentItemsPropertySet of the selected item. The above code shows how to extract them.

The PinnedChanged event is called: 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 changes the pinned status of several items, the e.ChangedPinnedIndices argument contains a list of indices to RecentItems where the Pinned status was changed.

⚠️ **GitHub.com Fallback** ⚠️