|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWith controls that present large quantities of distinct items, providing a history of user's selections might enhance user pleasure. Especially, since (s)he would be accustomed to this feature from the web browser experience. This article presents a BackgroundThe class handles the functionality of the buttons, their appearance is not its responsibility. Updating the class with new selections is accomplished by assigning the current selection to the Public InterfaceWith the provided comments, I hope that the usage would be self-explanatory: public sealed class History<T> : IDisposable where T : class
{
// Occurs when user chose from menu or clicked button.
public event EventHandler<HistoryEventArgs<T>> GotoItem
// Represents the method which retrieves the menu text for an item.
// If delegate is null, item's ToString method is used.
public delegate string GetHistoryMenuText(T item)
// Represents the method which retrieves the menu image for an item.
// If delegate is null, none is used.
public delegate Image GetHistoryMenuImage(T item)
// forward button may be null
// limitList == 0 for unlimited history
public History(ToolStripSplitButton back,
ToolStripSplitButton forward, uint limitList)
// assign new selected item on every change
public T CurrentItem { get; set; }
public void Clear()
public bool AllowDuplicates { get; set; }
public string GetHistoryMenuText MenuTexts { set; }
public Image GetHistoryMenuImage MenuImages { set; }
}
// Provides data for the History<T>.GotoItem event
public class HistoryEventArgs<T> : EventArgs
{
public HistoryEventArgs(T item)
public T Item { get; }
}
Using the CodeThe provided demo shows the most simplistic use with a private TreeView treeView;
private ToolStripSplitButton tssbBack;
private ToolStripSplitButton tssbForward;
private History<TreeNode> history;
// in form's constructor or Load eventhandler
{
history = new History<TreeNode>(tssbBack, tssbForward, 8);
history.GotoItem += new
EventHandler<HistoryEventArgs<TreeNode>>(history_GotoItem);
// anonymous methods are cool
history.MenuTexts = delegate(TreeNode node) { return node.Text; };
history.MenuImages = delegate(TreeNode node) { return
treeView.ImageList != null && !string.IsNullOrEmpty(node.ImageKey)
? treeView.ImageList.Images[node.ImageKey] : null; };
}
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
history.CurrentItem = treeView.SelectedNode;
}
private void history_GotoItem(object sender,
HistoryEventArgs<TreeNode> e)
{
treeView.SelectedNode = e.Item;
}
protected override void Dispose(bool disposing)
{
if (disposing && history != null)
{
history.Dispose();
}
base.Dispose(disposing);
}
Last, but not the least, the item Points of Interest
History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||