The KryptonCommand is not an analog for an existing WinForms component. With the proliferation of controls for showing and initiating actions it makes sense to simplify this common task. This is where the KryptonCommand comes into play.

Instead of setting the same state into multiple controls you just assign the state to a single instance of the KryptonCommand and then assign the command object to the controls. Now when you update the command it automatically updates all the controls for you. Here are a set of Krypton Toolkit controls placed on a KryptonPanel

I now create a KryptonCommand and set its image to be that of an angel and the text properties to something simple. Assigning the command instance to each controls KryptonCommand property updates them to the following…

Now any change to the command object is reflected in all the controls immediately. So if I change the commands Checked property to be True you get the following…

Obviously not all the controls have the ability to represent a checked state, so only the ones where it is relevant are updated. For example the label controls obviously cannot be checked and so ignore this setting.

Notice at the bottom that you can assign the command to individual ButtonSpec instances. This applies to button specs on the Ribbon, Navigator and all relevant Toolkit controls. This is a two way process, so if you click a button that understands the checked property then it alters the command to reflect this checked state change.

Relevant KryptonRibbon elements have also been updated to the use the command. Here we have a ribbon group with some standard elements being displayed…

And once the command is assigned…

With the KryptonCommand.Checked = True

If I now clicked on the first button element so that it was no longer checked then the other checked items would automatically become unchecked as well. This is because any change is reflected back to the command instance and all other controls attached to the command are therefore updated as well.  Although not shown above, the quick access toolbar buttons also take the command object.

The KryptonCommand issues a couple of events so that you can hook into a change in any of the properties and also be notified whenever the command needs to be executed. This allows the real power of the components to be unleashed.

A typical scenario might be handling the print action within your application. You create a command that has the appropriate text and images for indicating this print option. Then you can assign this command to a ribbon quick access toolbar button, a ribbon group button, a standalone button inside the client area and even to a button spec on a header. Whenever any of these elements is clicked you get the Execute event and can perform the actual printing action. If you decide to disable printing then just set the command Enabled property to False and all are updated immediately.

Now you can easily update and retain state without having to worry about updating every relevant control in your application. Once your application starts to grow this can become a real headache unless you deliberately manage this issue. Well now you can handle it with ease!

7 Responses to “KryptonCommand”

  1. Waescher Says:

    VERY good idea!!

  2. Cornel Says:

    This concept is the same with the one from WPF commands?

    Cornel

  3. Phil Wright Says:

    Similar to the idea of a Command and RoutedCommand in WPF but not quite. The WPF command only has a Execute and CanExecute pair of properties which is very generic but does not provide the text/image properties the Krypton version has.

  4. Cornel Says:

    Congrats!

  5. Arnold Says:

    that sounds very good and similar to delphi actions.
    Is there soon a possibilitiy to integrate it into a Prerelease?

  6. Metal-Frog Says:

    Hi,
    I use this concept to reflect the changes of the enabled state of pulldown menu- and context menu items.
    If KryptonCommand could help for this I will love it.
    Great idea!

  7. s0udk0stik Says:

    Hello, I have one question, is there any ability to know which control olds the buttonspec to which the command is assignated in the Execute event ?
    I made a command “Calc”, and added a buttonspec on several textboxes. All buttonspecs have the same command assigned to. When I click, it should open a calculator, then put the value in the textbox in which the user clicked the buttonspec. Thank you…

Leave a Reply