An ActionList for .NET 2.0
A little update on what I've been playing with in the .NET world recently.
Back in Delphi 3 (released around 1997 if memory serves), Borland introduced a great little component called TActionList. This component revolutionized the way we designed our applications, because it centralized all the UI logic in one place.
The concept, for those who never used Delphi, is that a TActionList is a collection of TAction components. Each TAction has properties you'd expect to see on a button or menu item - text, tooltip, enabled, visible etc etc. Every UI control in Delphi had an "Action" property, so that you could "hook them up" to the TAction they pointed to.
What made this revolutionary is that all your UI logic could be kept in one place. If you needed to lock the user out of a specific action, you'd simply disable that TAction component, and every menu item, toolbar button, link label etc would disable along with it. Likewise for other properties like Visible. TActionList even gave you a convenient "OnUpdate" event in which you could do all this UI updating.
The .NET framework, unfortunately, has no such component. If you have a MenuItem and ToolStripButton that both do the same thing, you have to remember to enable/disable both of them. WPF (formerly "Avalon") apparently introduces a new "command framework" that does this sort of stuff ... but that's no help to all us WinForms coders out there.
So I decided to write my own.
Basically I'm using the ExtenderProvider framework to add an "Action" property to all the UI controls that implement IButtonControl, and all the ToolStrip items. You can drop an ActionList component onto your form, define the Actions within it, and catch its Update event to tell the UI when to enable or disable each action. Each Action component has an Execute event in which you define what happens when that action gets executed.
The great part about this is that you can use the action itself to go back and update the UI. Check out this example of a "View|Toolbar" menu item. I'll do it all in code so you can see what goes on.
private MadProps.Windows.Action viewToolbar; private void Form1_Load(object sender, EventArgs e) { // create a viewToolbar action viewToolbar = new MadProps.Windows.Action(); viewToolbar.Text = "Toolbar"; viewToolbar.ToolTipText = "Shows or hides the toolbar"; viewToolbar.Execute += viewToolbar_Execute; // create our actionlist and add viewToolbar to it MadProps.Windows.ActionList al = new MadProps.Windows.ActionList(); al.Actions.Add(viewToolbar); al.Update += al_Update; // assign the viewToolbar action to the "View|Toolbar" menu item al.SetAction(viewToolbarMenuItem, viewToolbar); } void al_Update(object sender, EventArgs e) { viewToolbar.Checked = toolStrip1.Visible; } void viewToolbar_Execute(object sender, EventArgs e) { toolStrip1.Visible = !viewToolbar.Checked; }
So now we have a fully-functioning "View|Toolbar" menu item, whose "Checked" property is kept up-to-date by the action itself.
I'll continue to refine this component - I want to use it in Comicster. If you would like a copy, let me know.

# Trackback from Jens Rieck on 23/04/2006 8:04 AM
Comments
# Gollumstein
20/03/2006 11:16 AM
Yes, ActionList is really useful component, but Delphi ActionList is not quite ideal. We have add some shortcuts features. Try it ;)
http://nxsharp.com/overview/features/
# mabster
20/03/2006 11:23 AM
Interesting. I see you've gone with an "Image" property. I opted not to do that, because the image size often differs between a ToolStripMenuItem and the corresponding ToolStripButton. I guess you could introduce a "LargeImage" and "SmallImage" property to distinguish between the two, but I found it easier just to leave the image assignment to the menu items and toolbars.
# Gollumstein
20/03/2006 12:16 PM
Thanks, it's a good suggestion. We'll think about it.
# Dmitry (pestov AT gmail.com)
3/04/2006 4:11 AM
Hi!
ActionList is very usefull component in Delphi. I learning VB.NET now and my first question was - 'Where is actions?'. Do you plan to publish your component?
----
Dmitry
# mabster
3/04/2006 6:52 AM
Sure, Dmitry!
I will post a new "article" all about the ActionList component I have made and make it available there.
If I don't get distracted, I'll do it this week some time.