Click here to Skip to main content
13,247,448 members (73,903 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 6 Sep 2008

Global MenuCommand Verb in Visual Studio designers

, 6 Sep 2008
Rate this:
Please Sign up or sign in to vote.
Introduces to a hack to add global Context Menu Command to Visual Studio designer


Many .Net component verndor requires to add global context menu command to Visual Studio, which should be available whenever a required DesignerHost (Windows Forms, Web Forms, User Control etc. ) with corresponding form is active. However, .Net framework doesn't provide any method to add global context menu command permanently.
It has IMenuCommandService, whose AddVerb() method boasts to add global verb. But it is actually specific to the Component, for which IMenuCommandService is used. Whenever selection of control changes to something else, the verb is lost. I have developed a hack to introduce this capability flawlessly into .Net framework.


When I was developing my winforms layout manager Called 'SmartLayouts', I had exactly this requirement. I required it because I needed to provide ease of use to my users for layouting task. A toolbutton, Menu Command could do it, but it looks hassle in long term to users. After several days of intense research I found the trick of creating global verb which stays in Context Menu of desired DesignerHost.
As global level commands has to be visual studio plugin to be activated with visual studio startup, my solution too is a Visual Studio plug-in.

The Method

For Adding the Verb, you first need to ensure that correct DesignerHost is loaded. This is ensured by Comparing IDesignerHost.RootComponent with System.Windows.Forms.Form (you can use others according to your requirement). After that, We get ISelectionService from GetService Method.

ISelectionService sel = host.RootComponent.Site.GetService(typeof(ISelectionService)) as ISelectionService;

We attach an EventHandler to ISelectionService.SelectionChanged. That event handler is the crux of the hack. Whenever Selection changes, the eventHandler executes to Add the required verb back to IMenuCommandService verb list. The event handler containes code similar to following.

IMenuCommandService mcs = host.RootComponent.Site.GetService(typeof(IMenuCommandService)) as IMenuCommandService;
if (!mcs.Verbs.Contains(verb)) 

Now, the result is that you get the desired Verb whenever selection changed to designer of any other component. Overall, whenever you right click, you have the desired command in context menu!

Source Code

The source code of sample project is given. However, you need to add the ContextMenuCommand.Addin in your addin folder(usually in My Documents\Visual Studio 2005\Addins) and edit its xml node <assembly> to point to real ContextMenuCommand.dll.
<Assembly>C:\Documents and Settings\vkhaitan\My Documents\Visual Studio 2005\Projects\ContextMenuCommand\ContextMenuCommand\bin\ContextMenuCommand.dll</Assembly>
All the Documents and Source code are supplied with license The Code Project Open License (CPOL).


It would have been better, if .Net framework provided method for this and we didn't need to resort to these hacks. However, in practice, I found that this hack works flawlessly and never shown me any kind of problem or bug. So you can use it whenever you need it.


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


About the Author

Software Developer Smart Components
India India
Owner of Smart Components, producer of Professional Layout Manager SmartLayouts for Visual Studio 2005/2008

Check out SmartLayouts at

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 7 Sep 2008
Article Copyright 2008 by vkhaitan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid