Implementing the ribbon in Hoot for me was a simple but involved process. I converted from a multi-form window interface while also implementing interface inheritance.  One of my biggest challenges using the ribbon was determining how to use the Recent Items list. You can create items in design mode but that is not the purpose of the feature. Using Designer I got some insight into the recent item structure, but it took a while to see how to use it. 

Although recent items have to added before they are accessed, the code sections are listed in the order they would appear in the program, with the main ribbon actions listed first.

Recent items

With my implementation I created a new class with properties of the recent item that I would use.

The Recent Item

public RecentItem(string tag, string text)
{
ItemID = 0;
ItemTag = tag;
ItemText = text;
UserID = Properties.Settings.Default.User;
}

The text is the filename. The tag indicates the type of item since the items are not identified by file extension. ItemID will be updated when saved to the database. The database fields are similar to the class fields. 

Loading items on ribbon form load:

 private void AddRecentItems(List<RecentItem> recentItems) {
foreach (RecentItem item in recentItems)
DBManager.OrbAddRecentItem(item); // see below
}

Since the purpose of recent items is to revisit a file, it needs to be stored and restored when running the program again. I use a List<T> to store and manage the items. 

.. 
List<RecentItem> load = new List<RecentItem>();
load = DBManager.LoadRecentItems();
AddRecentItems(load); // see below
ribbonMain.OrbDropDown.RecentItems.Reverse();
..

With items saved chronologically in the database, it's necessary to reverse the list to put latest items at the top. The ID is set by the database using autonumber so the oldest items have the lowest ID and are loaded first.

Utility function to add items from the database

public static void OrbAddRecentItem(RecentItem recent)
{
frmHootGold top = (frmHootGold)System.Windows.Forms.Application.OpenForms["frmHootGold"];
RibbonOrbRecentItem adder = new RibbonOrbRecentItem();
adder.Tag = recent.ItemTag;
adder.Text = recent.ItemText;
adder.Value = recent.ItemID.ToString();
top.ribbonMain.OrbDropDown.RecentItems.Add(adder);
adder.Click += new System.EventHandler(top.OrbRecent_Click); // see below
}

When the items are added to the list, either as new items or when loaded from the database, the click event is set.

Calling Form

In response to user interaction, the actual addition of a recent item is done from one of the child windows with code segment something like this

RecentItem adder = new RecentItem("SavedSearch", searchFile);
DBManager.AddRecentItem(adder);
DBManager.OrbInsertRecentItem(adder); 

A separate utility method is used when actually adding a new item in response to user interaction. This has one difference from the database load because the list that has already been sorted in descending order.

 public static void OrbInsertRecentItem(RecentItem recent)
..
top.ribbonMain.OrbDropDown.RecentItems.Insert(0, adder);

The Recent Item List

When a user clicks an item on the Recent list, the following method uses the trigger and parameters mentioned earlier to pass that information to a new child form.

public void OrbRecent_Click(object sender, EventArgs e)
{
RibbonOrbRecentItem item = (RibbonOrbRecentItem)sender;
Form form = new Form();
switch (item.Tag.ToString())
{
case "SavedSearch": form = new frmCombo(); break;
case "Textfile": form = new frmTextfiles(); break;
}
OpenChild(form);
MDIUtils.SetParms(item.Text, form);
MDIUtils.SetTrigger((string)item.Tag, form);
}

One of the things I learned about Recent Items is that there is no obvious way to enable user-deletion of items. I notice Word doesn't have that either, nor does Windows, so I decided not to worry about it.

If you use this in a program you may notice that the items are added to Windows list of Recent Items, but it's not because of this method. It's simply because they were files that "you" opened. Anyway, this is my contribution to the C# Ribbon knowledge base.

Last edited Fri at 9:44 PM by dbell154, version 2

Comments

No comments yet.