Click here to Skip to main content
Click here to Skip to main content

Zeta Producer Command Routing

By , 14 Mar 2010
Rate this:
Please Sign up or sign in to vote.
ZetaProducerCommandRouting-Source

Introduction

When developing a real-world application in Windows Forms .NET, you usually have multiple menus, tool bars, ribbons, context menus, etc. The challenge here is to provide central handlers to perform actions (e.g. an action to open a file) that came from various sources (e.g. the user clicks a menu item, or a tool bar item).

Usually you call this "command routing" or "event bubbling".

This article introduces a small command routing library that contains code that I extracted from one of our commercial products.

Past, Present and Future of Command Routing

Following is a short overview of some of the solutions of the past, the present and the (my) future.

MFC

In the pre-.NET days, there were the Microsoft Foundation Classes (MFC) that came with what they call "Command Routing".

In MFC's command routing, the framework was responsible for correctly dispatching to the correct handler. In the handler, you could specify (among other things):

  • Whether the command's visual representation (a button, a menu item, ...) is displayed in enabled or disabled state.
  • The action to perform when the command is being executed.

The dispatching took place automatically, depending on the current focused window.

.NET Windows Forms

Now when Windows Forms appeared, they completely lacked this command routing framework.

Therefore I developed my own minimal version that is nearly-bug-free for some projects. You can download the library together with an example project at the top of this article.

There are different approaches by other developers to solve the lack of command routing in Windows Forms. I found the following ones through some Googling:

Windows Presentation Foundation (WPF)

To be honest, I do have no practical experiences in WPF.

From what I do understand, WPF does have very decent command routing. Therefore I tried to investigate whether it would be possible to extract parts of the WPF command routing and use it in Windows Forms. Unfortunately, I failed and ended with the solution you can download in this article. (Here is a German discussion about that topic.)

The Library and the Example Application

The Library

The main class being responsible for the command routing is the UICommandRoutingEngine class. Usually you create one instance per (main) form.

Both the (main) form and all user controls that want to be part of the event bubbling chain/tree do implement the IUICommandRoutingTarget interface and register themselves in the command routing engine instance by calling the UICommandRoutingEngine.AddTarget() method.

The interface being implemented by the (main) form and the controls has the following members:

/// <summary>
/// The interface for enabling/disabling menu items
/// and handling menu commands.
/// </summary>
public interface IUICommandRoutingTarget
{
    /// <summary>
    /// Tells whether to enable/disable the menu item or tool button.
    /// </summary>
    /// <param name="isEnabled">Indicates whether to enable or disable
    /// the command.</param>
    /// <param name="command">The command.</param>
    /// <param name="args">The arguments.</param>
    /// <returns></returns>
    UICommandResultInformation CheckCommandEnabled(
        ref bool isEnabled,
        UICommand command,
        UICommandEventArgs args );
    
    /// <summary>
    /// Executes the given command.
    /// </summary>
    /// <param name="command">The command.</param>
    /// <param name="args">The arguments.</param>
    /// <returns></returns>
    UICommandResultInformation ExecuteCommand(
        UICommand command,
        UICommandEventArgs args );
    
    /// <summary>
    /// Update UI states.
    /// </summary>
    void UpdateUI();
    
    /// <summary>
    /// The control in concern.
    /// </summary>
    /// <value>The observable control.</value>
    Control ObservableControl
    {
        get;
    }
}

The command routing engine is using these methods to query a control/form for the state of a command as well as to tell the control to actually execute a specific function.

An implementing form/control only handles those commands that it is actually responsible for. All other command are ignored by returning UICommandResultInformation.ContinueRouting which tells the engine to continue bubbling the event to the parent in the hierarchy.

Probably you'll get into the ideas of the library fastest when looking at the example application that ships with the source download.

The Example Application

In the example application, there is one main form which implements the IUICommandRoutingTarget interface and two user controls that both implement the IUICommandRoutingTarget interface, too.

When running the application, you see the tool bar buttons getting enabled and disabled as you change the focus between the text boxes and entering/modifying text.

Summary

This article introduced a small command routing library together with an example Windows Forms application.

While the command routing library should be nearly bug free, the example application still contains some issues like buttons working on the wrong textbox. I will improve that in the future, I do hope that it still helps you to show you some of the ideas I had when developing the library.

Of course I would be very happy if you contribute fixes, additions and enhancements to the library. To ask questions, suggest features or provide other comments, please use the comments section at the bottom of this article.

History

  • 2010-03-14 - First release to CodeProject

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.
 
In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
 
Some cool, free software from us:
 
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email
Some random fun stuff in German

Comments and Discussions

 
GeneralGreat, but found one quirk Pinmembersascha-x17-Mar-10 1:00 
GeneralRe: Great, but found one quirk PinmvpUwe Keim17-Mar-10 1:03 
GeneralGreat PinmemberMarcelo Ricardo de Oliveira15-Mar-10 10:42 
GeneralRe: Great PinmvpUwe Keim15-Mar-10 10:56 
GeneralI like it man PinmvpSacha Barber14-Mar-10 23:39 
GeneralRe: I like it man PinmvpUwe Keim14-Mar-10 23:42 
GeneralInteresting discussion PinmentorHans Dietrich14-Mar-10 14:53 
GeneralRe: Interesting discussion PinmvpUwe Keim14-Mar-10 23:42 
GeneralRe: Interesting discussion PinmentorHans Dietrich14-Mar-10 23:57 
I like how you have the Zeta-Test.com banner right behind you, framed by the webcam. Maybe it should be moved a little bit to the right, so more of it will be visible. Smile | :)
Best wishes,
Hans
 

[Hans Dietrich Software]

GeneralRe: Interesting discussion PinmvpUwe Keim15-Mar-10 0:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 14 Mar 2010
Article Copyright 2010 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid