Click here to Skip to main content
12,998,483 members (86,233 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 18 Apr 2008

Connect Multiple UI Elements to a Single Command Object

, 18 Apr 2008
Rate this:
Please Sign up or sign in to vote.
An easy way to group several UI elements (menu items, toolbar buttons, etc.) so they all execute the same command and are enabled/disabled together (with Visual Studio designer support).


It's very common to have both a menu item and a toolbar button that perform the same action when clicked. Sometimes, there is even a third or fourth control (say a button and a context menu item) that do the same thing. These controls need to be enabled and disabled together.

The code in this article allows each such control to be connected to a UICommand object by setting a property in the Visual Studio designer. The UICommand object has an Enabled property that enables or disables all of the associated controls. It also has an Execute event that is raised when any of the controls are clicked.

Using the code

Download the demo code. The CommandDemo project contains two classes, UICommand and UICommandProvider. Copy the associated files into your project and compile. The classes will appear in the toolbox as seen below whenever the form designer is open:


Begin by dragging an instance of UICommandProvider onto the form. It will appear in the components area of the designer (near the bottom), as seen in the above screenshot. Only one instance of UICommandProvider is needed per form. Because UICommandProvider implements IExtenderProvider and its CanExtend method returns true for any object derived from ToolStripItem or Control, the Visual Studio designer adds a new property to those objects. The property is of type UICommand.

The demo project contains two instances of UICommand: uiCommand1 and uiCommand2. Each of these objects is connected to four other controls: a regular button, a tool bar button, a menu item (under the Commands menu), and a context menu item. This was done by dragging two instances of UICommand onto the form, then setting each control's UICommand property to the appropriate instance of UICommand. For example, the following screenshot shows that button2.UICommand is set to uiCommand2:


The result is that whenever button2 (or any of the other controls whose UICommand property is set to uiCommand2) is clicked, the event uiCommand2.Execute is raised. Also, whenever uiCommand2.Enabled is set, the Enabled property of the associated controls is also set.

For the purposes of this demo, the Execute handler for each UICommand simply toggles the Enabled property of the other UICommand. This makes it easy to see that one line of code enables or disables several controls on the form. Here are the two event handlers:

private void uiCommand1_Execute(object sender, EventArgs e) {
    uiCommand2.Enabled = !uiCommand2.Enabled; 

private void uiCommand2_Execute(object sender, EventArgs e) {
    uiCommand1.Enabled = !uiCommand1.Enabled; 

UICommand could probably be extended to have an Image property that causes the associated controls to have the same image (where appropriate), a ToolTip property that assigns the same tooltip text to all the controls, and so on. I'll leave this as an exercise to the reader.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
Mark Lauritsen has been a software developer for as long as he can remember.

You may also be interested in...


Comments and Discussions

GeneralSound like the "Actions" from Delphi Pin
Member 393758721-Apr-08 21:16
memberMember 393758721-Apr-08 21:16 
GeneralRe: Sound like the "Actions" from Delphi Pin
Jeremy Thomas13-Jul-08 0:23
memberJeremy Thomas13-Jul-08 0:23 
GeneralGreat Idea! Pin
sam.hill20-Apr-08 6:08
membersam.hill20-Apr-08 6:08 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 18 Apr 2008
Article Copyright 2008 by MarkLTX
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid