-
Notifications
You must be signed in to change notification settings - Fork 19
RecentItems
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 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:
private Ribbon _ribbon;
private RibbonRecentItems _ribbonRecentItems;
List<RecentItemsPropertySet> _recentItems;
public Form1()
{
InitializeComponent();
_ribbon = new Ribbon();
_ribbonRecentItems = new RibbonRecentItems(_ribbon, (uint)RibbonMarkupCommands.cmdRecentItems);
_ribbonRecentItems.ExecuteEvent += new EventHandler<ExecuteEventArgs>(_recentItems_ExecuteEvent);
}
private void Form1_Load(object sender, EventArgs e)
{
InitRecentItems();
}
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. 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, ExecuteEventArgs e)
{
RecentItemsEventArgs args = RecentItemsEventArgs.Create(sender, e);
if (args.RecentItems != null)
{
for (int i = 0; i < args.RecentItems.Count; ++i) {
string label = args.RecentItems[i].Label;
string labelDescription = args.RecentItems[i].LabelDescription;
bool pinned = args.RecentItems[i].Pinned;
// update pinned value
_recentItems[i].Pinned = pinned;
}
}
if (args.SelectedItem != null)
{
SelectedItem<RecentItemsPropertySet> selected = args.SelectedItem;
int selectedItem = selected.SelectedItemIndex;
string label = args.SelectedItem.PropertySet.Label;
string labelDescription = args.SelectedItem.PropertySet.LabelDescription;
bool pinned = args.SelectedItem.PropertySet.Pinned;
}
int maxCount = RecentItems.MaxCount;
}
Older code:
void _recentItems_ExecuteEvent(object sender, ExecuteEventArgs e)
{
if (key.PropertyKey == RibbonProperties.RecentItems)
{
// go over recent items
object[] objectArray = (object[])e.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)e.CurrentValue.PropVariant.Value;
// get selected item label
PropVariant propLabel;
e.CommandExecutionProperties.GetValue(ref RibbonProperties.Label,
out propLabel);
string label = (string)propLabel.Value;
// get selected item label description
PropVariant propLabelDescription;
e.CommandExecutionProperties.GetValue(ref RibbonProperties.LabelDescription,
out propLabelDescription);
string labelDescription = (string)propLabelDescription.Value;
// get selected item pinned value
PropVariant propPinned;
e.CommandExecutionProperties.GetValue(ref RibbonProperties.Pinned,
out propPinned);
bool pinned = (bool)propPinned.Value;
}
}
I know, some explanations are in order. The ExecuteEvent event is called on two occasions:
- When the user clicks on one of the items.
- 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 e.CurrentValue argument contains the index of the selected item and e.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 e.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.
-
Basics
- Introduction, Background on the windows ribbon
- Basic Ribbon Wrapper Basic .NET wrappers for windows ribbon.
- Quickstart Tutorial
- First WinForms Ribbon Application How to create an empty WinForms application with ribbon support.
-
Working with Ribbon Controls
- Application Menu with Buttons How to use the ribbon application menu.
- Application Menu with SplitButton and DropDownButton How to use the ribbon application menu with ribbon split button and ribbon dropdown button controls.
- Tabs, Groups and HelpButton How to use ribbon tabs, groups and the ribbon help button control.
- Spinner How to use the ribbon spinner control.
- ComboBox How to use the ribbon combo box control.
- DropDownGallery, SplitButtonGallery and InRibbonGallery How to use the ribbon drop down gallery, split button gallery and in ribbon gallery controls.
- CheckBox and ToggleButton How to use the ribbon check box and toggle button controls.
- DropDownColorPicker How to use the ribbon drop down color picker control.
- FontControl How to use the ribbon font control.
- ContextualTabs How to work with ribbon contextual tabs.
- ContextPopup How to work with ribbon context popup.
- RecentItems How to work with ribbon recent items control.
- QuickAccessToolbar How to work with the ribbon quick access toolbar.
- The Ribbon Class How to work with the ribbon class. Methods, Properties, Events
- EventLogger Since Windows 8: Logging ribbon events
- UICollectionChangedEvent How to work with the ChangedEvent in an IUICollection
-
Working with miscellany Ribbon features
- ApplicationModes How to work with ribbon application modes.
- SizeDefinition How to define custom size definitions for ribbon group elements.
- Localization How to localize a ribbon.
- Changing Ribbon Colors How to change the ribbon colors.
- Working with Images How to work with images in the ribbon.
- Use Ribbon as External DLL How to load ribbon resources from external DLLs.
- Wrapper class RibbonItems An auto generated wrapper class from the ribbon markup.
-
Designing, building, previewing Windows Ribbon with RibbonTools
- RibbonTools basics Settings, Command line, ...
- Create a new project Create a WordPad sample project
- Preview the Ribbon
- Specifying Ribbon Commands
- Designing Ribbon Views
- Convert Images to Alpha Bitmaps
-
Modeling Guidelines
-
How to ...