Geeks With Blogs
MightyZot

Infragistics controls are great for fast and cool UI development. They have controls for toolbars, menus, docking, etc. The Infragistics toolbars and menus are integrated, allowing you to use a single tool for both menus and toolbar buttons. That’s awesome!  One thing that has always frustrated me though, is the event model.

After setting up your tools, you handle events in one of two ways (that I have found.) Using the most obvious method, you can add a ToolClick handler to the UltraToolbarManager’s ToolClick event like this:

ToolbarManager.ToolClick += ToolbarManager_ToolClick;

Your handler then looks like so:

void ToolbarManager_ToolClick(object sender, ToolClickEventArgs e)
{
    switch (e.Tool.Key)
    {
        case "Whatever":
            // do whatever
            break;

        case "Another Whatever":
            // do whatever
            break;
    }
}

Yuck! Obviously, this can lead to some very huge switch…case statements that just muddy up your code and make it difficult to maintain. You can avoid this by assigning click handlers to individual tools like this:

ToolbarManager.Tools[“My Key”].ToolClick += My_Key_Click;

That solution is not so bad, but now you’ll have a whole bunch of those assignments muddying up your code. What if I could give you a way to create event handlers and hook them up automatically, such that all you’ve got to do is write the event handler?

Check this out! Using this class, all you need to do is pass it your UltraToolbarsManager and your form, decorate your event handlers with the ToolClickHandler attribute and it takes care of hooking up your event handlers to the tools in the Infragistics toolbar based upon the method names and the keys.

public class ToolClickManager
{
    public ToolClickManager(UltraToolbarsManager toolbars, Control sink)
    {
        Debug.Assert(toolbars != null);
        Debug.Assert(sink != null);
        this.Connect(toolbars, sink);
    }

    private void Connect(UltraToolbarsManager toolbars, Control sink)
    {
        Type sinktype = sink.GetType();
        MethodInfo[] handlers = this.GetHandlers(sinktype);
        foreach (MethodInfo handler in handlers)
        {
            string[] parts = handler.Name.Split('_');
            int ubound = parts.GetUpperBound(0);
            if (parts.GetLength(0) >= 2 && string.Compare(parts[ubound], "click", true) == 0)
            {
                string tool = parts[ubound - 1];
                if (toolbars.Tools.Exists(tool))
                {
                    ToolBase toolref = toolbars.Tools[tool];
                    toolref.ToolClick += handler.CreateDelegate(typeof(ToolClickEventHandler), sink) as ToolClickEventHandler;
                }
            }
        }
    }

    private MethodInfo[] GetHandlers(Type type)
    {

       IEnumerable<MethodInfo> methods = type.GetRuntimeMethods().Where(m => m.GetCustomAttribute(typeof(ToolClickHandlerAttribute)) != null);
        return methods.ToArray();
    }
}

[AttributeUsage(AttributeTargets.Method)]
public class ToolClickHandlerAttribute : Attribute
{
}

Hooking up your events works like this:  Step #1 is to instantiate the ToolClickManager in your form’s constructor like this:

ToolClickManager clickmanager = null;

public MainForm()
{
    InitializeComponent();
    this.clickmanager = new ToolClickManager(this.ToolbarManager, this);
}

To attach events to your tools that make up your menus and toolbars, create event handlers and decorate them like this:

[ToolClickHandler]
void File_Exit_Click(object sender, ToolClickEventArgs e)
{
    this.Close();
}

“File” matches my group in this case and I’m just using that to organize my tools. It is not required. “Exit” is the key for the tool and I’m checking for “Click” in the hookup code just to constrain the method names a bit. This example could be expanded to do parameter checking, allow you to specify the key name in the ToolClickHandlerAttribute implementation, and to support more events like “DoubleClick”.

This will make our code a little more compact!

Posted on Wednesday, January 9, 2013 4:55 PM C# Tips and Tricks | Back to top


Comments on this post: ToolClickManager for Infragistics

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © MightyZot | Powered by: GeeksWithBlogs.net