<p>I'm working on a WinForm .Net application with the basic UI that includes toolbar buttons, menu items and keystrokes that all initiate the same underlying code. Right now the event handlers for each of these call a common method to perform the function.</p>

<p>From what I've read this type of action could be handled by the <a href="http://www.dofactory.com/Patterns/PatternCommand.aspx" rel="noreferrer">Command design pattern</a> with the additional benefit of automatically enabling/disabling or checking/unchecking the UI elements.</p>

<p>I've been searching the net for a good example project, but really haven't found one. Does anyone have a good example that can be shared?</p>

[{'answer_id': 15103, 'author': 'ESV', 'author_id': 150, 'author_profile': 'https://Stackoverflow.com/users/150', 'pm_score': 1, 'selected': False, 'text': '<p>Try open source, .NET editors like <a href="http://sharpdevelop.net/OpenSource/SD/" rel="nofollow noreferrer">SharpDevelop</a> or <a href="http://notepad-plus.sourceforge.net/uk/site.htm" rel="nofollow noreferrer">Notepad++</a>.</p>\n\n<p>There is (naturally) some discussion of the Command Pattern at <a href="http://c2.com/cgi/wiki?CommandPattern" rel="nofollow noreferrer">http://c2.com/cgi/wiki?CommandPattern</a> that might be helpful.</p>\n'}, {'answer_id': 15207, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 5, 'selected': True, 'text': "<p>Let's first make sure we know what the Command pattern is:</p>\n\n<blockquote>\n  <p>Command pattern encapsulates a request\n  as an object and gives it a known\n  public interface. Command Pattern\n  ensures that every object receives its\n  own commands and provides a decoupling\n  between sender and receiver. A sender\n  is an object that invokes an\n  operation, and a receiver is an object\n  that receives the request and acts on\n  it.</p>\n</blockquote>\n\n<p>Here's an example for you.  There are many ways you can do this, but I am going to take an interface base approach to make the code more testable for you.  I am not sure what language you prefer, but I am writing this in C#.</p>\n\n<p>First, create an interface that describes a Command.</p>\n\n<pre><code>public interface ICommand\n{\n    void Execute();\n}\n</code></pre>\n\n<p>Second, create command objects that will implement the command interface.</p>\n\n<pre><code>public class CutCommand : ICommand\n{\n    public void Execute()\n    {\n        // Put code you like to execute when the CutCommand.Execute method is called.\n    }\n}\n</code></pre>\n\n<p>Third, we need to setup our invoker or sender object.</p>\n\n<pre><code>public class TextOperations\n{\n    public void Invoke(ICommand command)\n    {\n        command.Execute();\n    }\n}\n</code></pre>\n\n<p>Fourth, create the client object that will use the invoker/sender object.</p>\n\n<pre><code>public class Client\n{\n    static void Main()\n    {\n        TextOperations textOperations = new TextOperations();\n        textOperation.Invoke(new CutCommand());\n    }\n}\n</code></pre>\n\n<p>I hope you can take this example and put it into use for the application you are working on.  If you would like more clarification, just let me know.</p>\n"}, {'answer_id': 20747, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 2, 'selected': False, 'text': "<p>Your on the right track.  Basically you will have a model that represents the document.  You will use this model in the CutCommand.  You will want to change the CutCommand's constructor to accept the information you want to cut.  Then everytime, say the Cut Button is clicked, you invoke a new CutCommand and passing the arguments in the constructor.  Then use those arguments in the class when the Execute method is called.</p>\n"}, {'answer_id': 20801, 'author': 'Imran', 'author_id': 1897, 'author_profile': 'https://Stackoverflow.com/users/1897', 'pm_score': 1, 'selected': False, 'text': '<p>Qt uses Command Pattern for Menubar/Toolbar items.</p>\n\n<p>QActions are created seperately from QMenuItem and QToolbar, and the Actions can be assigned to QMenuItem and QToolbar with setAction() and addAction() method respectively.</p>\n\n<p><a href="https://derefer.it/http://web.archive.org/web/20100801023349/http://cartan.cas.suffolk.edu/oopdocbook/html/menus.html" rel="nofollow noreferrer">http://web.archive.org/web/20100801023349/http://cartan.cas.suffolk.edu/oopdocbook/html/menus.html</a></p>\n\n<p><a href="https://derefer.it/http://web.archive.org/web/20100729211835/http://cartan.cas.suffolk.edu/oopdocbook/html/actions.html" rel="nofollow noreferrer">http://web.archive.org/web/20100729211835/http://cartan.cas.suffolk.edu/oopdocbook/html/actions.html</a></p>\n'}, {'answer_id': 34513005, 'author': 'Dzianis Yafimau', 'author_id': 3877717, 'author_profile': 'https://Stackoverflow.com/users/3877717', 'pm_score': 0, 'selected': False, 'text': "<p>I can't help you with example link, but can provide example by myself.</p>\n\n<p>1) Define ICommand interface:</p>\n\n<pre><code>public interface ICommand {\n   void Do();\n   void Undo();\n}\n</code></pre>\n\n<p>2) Do your ICommand implementations for concrete commands, but also define abstract base class for them:</p>\n\n<pre><code>public abstract class WinFormCommand : ICommand {\n\n}\n</code></pre>\n\n<p>3) Create command invoker:</p>\n\n<pre><code>public interface ICommandInvoker {\n  void Invoke(ICommand command);\n  void ReDo();\n  void UnDo();\n}\n\npublic interface ICommandDirector {\n  void Enable(ICommand);\n  void Disable(ICommand);\n}\n\npublic class WinFormsCommandInvoker : ICommandInvoker, ICommandDirector {\n\n    private readonly Dictionary&lt;ICommand, bool&gt; _commands;\n    private readonly Queue&lt;ICommand&gt; _commandsQueue;       \n    private readonly IButtonDirector _buttonDirector;\n\n    // you can define additional queue for support of ReDo operation\n\n    public WinFormsCommandInvoker(ICommandsBuilder builder, IButtonDirector buttonDirector) {\n      _commands = builder.Build();\n      _buttonDirector = buttonDirector;\n      _commandsQueue = new Queue&lt;ICommand&gt;();\n    } \n\n    public void Invoke(ICommand command) {\n        command.Do();\n        __commandsQueue.Enqueue(command);\n    }\n\n    public void ReDo() {\n      //you can implement this using additional queue\n    }\n\n    public void UnDo() {\n      var command = __commandsQueue.Dequeue();\n      command.Undo();\n    }\n\n    public void Enable(ICommand command) {\n      _commands.[command] = true;\n      _buttonDirector.Enable(command);\n    }\n\n    public void Disable(ICommand command) {\n      _commands.[command] = false;\n      _buttonDirector.Disable(command); \n    }\n}\n</code></pre>\n\n<p>4) Now you can implement your ICommandsBuilder, IButtonDirector and add other interfaces such as ICheckBoxDirector to WinFormsCommandInvoker.</p>\n"}]
['https://Stackoverflow.com/questions/15047', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1752/']