Click here to Skip to main content
6,594,932 members and growing! (14,490 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate License: The Code Project Open License (CPOL)

Global MenuCommand Verb in Visual Studio designers

By vkhaitan

Introduces to a hack to add global Context Menu Command to Visual Studio designer
C#, VB, Windows, Visual Studio (VS2005, VS2008), Dev
Posted:6 Sep 2008
Views:4,825
Bookmarked:11 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 2.61 Rating: 4.33 out of 5

1
1 vote, 25.0%
2

3

4
3 votes, 75.0%
5
ContextMenuCommand

Introduction

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.

Background

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)) 
{  
      mcs.Verbs.Add(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).

Conclusion

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.

License

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

About the Author

vkhaitan


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

Check out SmartLayouts at http://www.smart-components.com/
Occupation: Software Developer
Company: Smart Components
Location: India India

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Sep 2008
Editor:
Copyright 2008 by vkhaitan
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project