Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / Windows Forms

Zeta Producer Command Routing

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
14 Mar 2010CPOL4 min read 35.1K   598   24   10
Introducing a small UI command routing (event bubbling) library for Windows Forms .NET
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:

C#
/// <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)


Written By
Chief Technology Officer Zeta Software GmbH
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.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralGreat, but found one quirk Pin
sascha-x17-Mar-10 1:00
sascha-x17-Mar-10 1:00 
As always great artice by Uwe, but while trying the demo app I found one quirk. Try typing some text and click 'Select all' via the contextmenu, the button state will not change, is this intented?
GeneralRe: Great, but found one quirk Pin
Uwe Keim17-Mar-10 1:03
sitebuilderUwe Keim17-Mar-10 1:03 
GeneralGreat Pin
Marcelo Ricardo de Oliveira15-Mar-10 10:42
mvaMarcelo Ricardo de Oliveira15-Mar-10 10:42 
GeneralRe: Great Pin
Uwe Keim15-Mar-10 10:56
sitebuilderUwe Keim15-Mar-10 10:56 
GeneralI like it man Pin
Sacha Barber14-Mar-10 23:39
Sacha Barber14-Mar-10 23:39 
GeneralRe: I like it man Pin
Uwe Keim14-Mar-10 23:42
sitebuilderUwe Keim14-Mar-10 23:42 
GeneralInteresting discussion Pin
Hans Dietrich14-Mar-10 14:53
mentorHans Dietrich14-Mar-10 14:53 
GeneralRe: Interesting discussion Pin
Uwe Keim14-Mar-10 23:42
sitebuilderUwe Keim14-Mar-10 23:42 
GeneralRe: Interesting discussion Pin
Hans Dietrich14-Mar-10 23:57
mentorHans Dietrich14-Mar-10 23:57 
GeneralRe: Interesting discussion Pin
Uwe Keim15-Mar-10 0:00
sitebuilderUwe Keim15-Mar-10 0:00 

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

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