Click here to Skip to main content
Click here to Skip to main content
Go to top

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).

Introduction

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:

CommandDemoDesigner.PNG

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:

CommandDemoProperty.PNG

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.

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralSound like the "Actions" from Delphi PinmemberMember 393758721-Apr-08 21:16 
GeneralRe: Sound like the "Actions" from Delphi PinmemberJeremy Thomas13-Jul-08 0:23 
GeneralGreat Idea! Pinmembersam.hill20-Apr-08 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 18 Apr 2008
Article Copyright 2008 by MarkLTX
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid