Click here to Skip to main content
Click here to Skip to main content

VS.NET Change Case Add-In

By , 19 Apr 2005
Rate this:
Please Sign up or sign in to vote.

Sample Image - ChangeCaseAddIn.jpg

Introduction

In this article, I would create an add-in to toggle the case of selected text. It explains both options of accessing the functionality from the Tools menu or the context menu.

History

I was really fantasized by the Add-Ins which are available on the net for Visual Studio .NET. I have been using VSPropertyGenerator for a long time and as a programmer I was always curious to know how it worked. I searched on the internet and was able to find the information but in a scattered way. So, it took me a while to write my own Add-In. To help others, I would explain step by step how to create an Add-In.

Note: Visual Studio provides the namespaces Microsoft.Office.Core, Extensibility and EnvDTE to access the DTE of Visual Studio.

Solution

Open Visual Studio .NET and create a new project. In the New Project window, select “Other projects-> Extensibility projects”, and select template as “Visual Studio .NET AddIns”.

Give a name, say “ChangeCaseAddIn”.

Sample screenshot

Press OK. Select the items in wizard as shown below:

Sample screenshot

Sample screenshot

Select Options -> “Tools” to display it in menu:

Sample screenshot

Click Next and press OK. You would have two projects names: “ChangeCaseAddIn” and “ChangeCaseAddInSetup”.

Now, open the connect.cs class in the ChangeCaseAddIn project. In the OnConnection method, add the following code:

if(connectMode == Extensibility.ext_ConnectMode.ext_cm_Startup)
{
    object []contextGUIDS = new object[] { };
    Commands commands = applicationObject.Commands;
    _CommandBars commandBars = applicationObject.CommandBars;
    try
    {
     // Enable this code if wants to display menu item in "Tools"
     //CommandBar commandBar = (CommandBar)commandBars["Tools"];
     // Enable this code to display options as context menu
     CommandBar commandBar = (CommandBar)commandBars["Code Window"];
     Command command = commands.AddNamedCommand(addInInstance, "ToggleCase", 
        "Toggle Case", "Changes selected text case", true, 59, ref contextGUIDS, 
        (int)vsCommandStatus.vsCommandStatusSupported+ 
        (int)vsCommandStatus.vsCommandStatusEnabled);
     CommandBarControl commandBarControl = command.AddControl(commandBar, 1);
    }
    catch(System.Exception /*e*/)
    {
    }

This code helps to add the toggle case in the “Tools” menu or in the context menu.

Modify QueryStatus method to:

if(commandName == "ChangeCaseAddIn.Connect.ToggleCase")
{
  status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | 
          vsCommandStatus.vsCommandStatusEnabled;
}

To display the menu item: change EXEC method to toggle the selected text case.

    TextDocument doc = 
       applicationObject.ActiveDocument.Object("TextDocument") as TextDocument;
    string text = doc.Selection.Text;
    if(commandName == "ChangeCaseAddIn.Connect.ToggleCase")
    {
      // Toggle case
      if(text == text.ToLower(System.Globalization.CultureInfo.CurrentCulture))
       doc.Selection.Text = 
          text.ToUpper(System.Globalization.CultureInfo.CurrentCulture);
      else
       doc.Selection.Text = 
          text.ToLower(System.Globalization.CultureInfo.CurrentCulture);
      break;
    }

When you build and create a setup for this application, you can only see a new option called “ToggleCase” in the context menu and without any image.

Sample screenshot

To display an image against it, you need to assign a bitmap to the add-in using a resource file. To do so, add a new project to this solution for displaying the icon\BMP against the menu item. Add a new project, select “Win32 project”:

Sample screenshot

Right click on “Resource File” folder in this project and select “Add New Item”. Add a new resource file to this folder:

Sample screenshot

Now, double click on the resource file added to the Open Resource view. In Resource view, right click on the resource file and add a bitmap using “Add Resource” option. Select the BMP file and change the property “ID” from IDB_BITMAP1 to 1 and language as US English.

Now, open the connect.cs class in the ChangeCaseAddIn project. In OnConnection method, modify the following code:

Command command = commands.AddNamedCommand(addInInstance, "ToggleCase", 
  "Toggle Case", "Changes selected text case", false, 1, ref contextGUIDS, 
  (int)vsCommandStatus.vsCommandStatusSupported + 
  (int)vsCommandStatus.vsCommandStatusEnabled);

Note we changed “true” to “false” and 59 to 1 in the AddNamedCommand method to display the bitmap created above.

Create setup again. Right click on the ChangeCaseAddInSetup project and select “File View”. Right click on the application folder and create a new folder named “1033” (for English language). Right click application folder again and select “Add Project Output”. On the dialog box, select “ChangeCaseAddIn” primary output.

Right click on the newly created folder “1033” and select primary project output of “ChangeCaseAddInUI”.

Sample screenshot

Right click on the ChangeCaseAddInSetup project again and select “Registry”. Add to a new string values to the “AddIn” key for handling bitmap displaying.

  • SatelliteDLLName: Add the VC++ DLL name and path.
  • SatelliteDLLPath: Path to DLL (user [TARGETDIR]).

Sample screenshot

Rebuild the solution and install the application.

Open a new project and you would see “ToggleCase” in the context menu. You could now select any text and toggle the case (upper and lower) by clicking on this item.

Sample screenshot

Modifying “OnConnection” method of the Connect class in ChangeCaseAddIn project would display this item in the Tools menu rather than in the context menu.

Sample screenshot

Conclusion

This concludes the article to create an add-in for Visual Studio .NET. I tried to explain in detail as much I could. For any apprehensions, please drop a message.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

S Sansanwal
Architect
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionHow to add submenu to 'Code Window' menu? PinmemberBehzad Ebrahimi17-Jul-08 19:16 
GeneralTip PinmemberDarth_Sulfur16-Jul-05 16:00 
GeneralGood article Pinmemberjulio36111-Jun-05 14:13 
GeneralExisting keyboard functionality... PinmemberGordon Mackie27-Apr-05 0:37 
GeneralRe: Existing keyboard functionality... PinmemberS Sansanwal18-May-05 11:20 
GeneralFix formatting Pinmemberleppie19-Apr-05 19:28 

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
Web02 | 2.8.140415.2 | Last Updated 19 Apr 2005
Article Copyright 2005 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid