(Print this page)

An Action List component
Published date: Sunday, August 12, 2007
On: Moer and Éric Moreau's web site

A couple of months ago, I was introduced to the Action List component and I found it very useful. It allows us to save a lot of code we need to write when we are trying to keep properties and/or react to events of some controls in sync.

This control is apparently well known from Delphi developers (as it comes out of the box) but .Net Framework developers don’t have anything similar to this. This is why some programmers (probably working with both environments) created an equivalent component for us to use in VB or C#.

The purpose of this component

This component is a kind of delegate that let you do operations like settings properties or reacting to events for more then a single GUI element of your form. This way, you can really decouple UI elements from the code.

You are already used to that technique for events handling where you set the same method to handles events of many control. This part is easy to do. But what if you want to set the Enabled property of multiple controls all to the same value? All this using a single line of code? For example, you can use this technique to have your toolbar and your menu always in sync. This is exactly where the Action list proves to be useful.

Not in your original toolbox

As I said before, nothing like this is existing out-of-the-box for .Net developers.

The one I will use here is one created by Serge Weinstock that you can download from CodeProject. The component was written in C# but I will show you how to use it from a VB application.

This component is an extender that adds new properties to existing control (much like the Tooltip extender). If you need a refresher on this topic, see my article titled “Extended providers” published in October 2004.

Adding the component to your toolbox

The first thing you need to do is to add the component to your toolbox so that you can add it to any form of your application easily.

The component can be downloaded along with its full C# source code from the here. It is also available in the _Assemblies folder from the downloadable file of this article.

Once you have the CDiese.DLL file available, right-click on your toolbox and select “Choose items…”. From the “Choose toolbox items” dialog that will be displayed on your screen, select the “Browse” button (from the “.NET Framework Components” tab that should be open by default) and navigate to your dll file. Ensure that the ActionList component is selected and click the OK button.

Figure 1: Adding the component to your toolbox

Creating the demo application

In a Windows Forms application, add a MenuStrip and a ToolStrip control. Using the smart tag (the small arrow at the top right of the control), insert the standard items for both the MenuStrip and the ToolStrip. If you need more details on those 2 controls, have a look at an article I have published in May 2006 titled The Strippers’ club.

Figure 2: Inserting standard items

While creating the user interface, we will also add some other controls. Add a new menu item (Custom Items) with 3 items underneath it, 3 news buttons on your ToolStrip, 2 buttons, and 1 checkbox on the form as shown in figure 3.

Figure 3: Adding some custom elements

Notice that no properties (except maybe the name) have been set so far and no code have been written.

Creating your first action items

We are now ready to drop our new component on this form. So go ahead and drop an ActionList component on your form (which will automatically go into the component tray at the bottom of the form).

Now open the Actions collection editor of the ActionList component and create 3 actions.

Figure 4: The action collection editor

Have you seen that we have access to properties like Text, Enabled, Checked, and Visible and so on? We will play with it a bit further.

Linking controls to action items

Now that we have controls and actions, we are ready to link them together.

Go back to your MenuStrip, select the first item you added to the menu, find the “Action on ActionList1” extender property and set it to the action named Action1. Repeat for the 2 other menu item.

Figure 5: Linking the Action property

Also do the same thing for the 3 buttons you have added to the ToolStrip control.

And finally, link the 2 buttons and the checkbox to the action.

You now have 9 controls that are bound to 3 actions.

Now, some code

If I give you instruction to disable 3 of those controls when pressing any of menu item 1 or toolbar button 1 or button 1, you would probably create a method that handles the Click event of those 3 controls. But you don’t need to do that now that you have an action bound to those 3 controls. You can use the Execute event of the action. From now on, any control bound to this action will have the code executed without adding any code.

And if I tell you to change a property like the Text property or the Enabled property of 3 of those controls, you can also benefit the action list to set the property of one action and every control bound to that action will be affected.

For example, I have added this code to the Execute method of action 1:

Private Sub Action1_Execute(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Action1.Execute
    Action2.Text = "Text has changed"
    Action3.Enabled = False
    Action3.Checked = True
End Sub
The result of those lines is that every control bound to Action 2 has its Text changed and that every control bound to Action 3 is disabled and checked.

Conclusion

Very often, we have multiple controls that we want to maintain as a single one. Instead of having to write code specifically for each control, you access a single action that may affect any number of controls.

Very cool and very easy to implement.

I hope you appreciated the topic and see you next month.


(Print this page)