All entries for Saturday 01 October 2005
October 01, 2005
I decided to resurrect my CommandProvider component today. It should useful for a big project I'm currently working on. The component centralises the idea of an application "command" such as "New", "Open" or "Save". Usually in small applications people just put code in a menu item click event handler. The problem here is then when mutliple controls can invoke the same code. You usually end up with many event handlers all having to call one method. In addition, controlling the enabled/disabled state of each UI control linked to the command becomes non-trivial – even with data binding.
So my solution is a component to drop on a form that contains a list of available commands. (Each command also has Enabled and Checked properties.) The component implements IExtenderProvider and provides a Command property to other controls on the form. In addition it provides a CommandInvoker property that should be linked to a control's Click event for example. (EnabledProperty and CheckedProperty are also provided).
The form code just has to handle the CommandInvoked event of the CommandProvider. This typically is a switch block over the e.Command.Name value.
So I have no annoying event handlers for every damn menu item and toolbar button! And even cooler I can write "commandProvider.Commands["Save"].Enabled = false;" and the component will disable any controls set to the Save command.
In addition, data binding to Command objects makes keeping the UI consistent as simple as : "statusBar1.DataBindings.Add("Visible", commandProvider.Commands["StatusBar"], "Checked");"
I am wondering if this component has any commercial viability. I have put a fair amount of time into it and I believe could certainly improve the quality of larger .NET winform apps. I will have to research the market and see what else is out there…