Click here to Skip to main content
15,883,870 members
Articles / Programming Languages / Visual Basic
Article

VS.NET Change Case Add-In

Rate me:
Please Sign up or sign in to vote.
4.03/5 (7 votes)
19 Apr 20053 min read 51K   247   36   6
This article describes step by step creation of an Add-In for VS.NET.

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:

C#
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:

C#
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.

C#
    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:

C#
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


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to add submenu to 'Code Window' menu? Pin
Behzad Ebrahimi17-Jul-08 19:16
Behzad Ebrahimi17-Jul-08 19:16 
GeneralTip Pin
Lord Kixdemp16-Jul-05 16:00
Lord Kixdemp16-Jul-05 16:00 
GeneralGood article Pin
julio36111-Jun-05 14:13
julio36111-Jun-05 14:13 
GeneralExisting keyboard functionality... Pin
Gordon Mackie27-Apr-05 0:37
Gordon Mackie27-Apr-05 0:37 
GeneralRe: Existing keyboard functionality... Pin
S Sansanwal18-May-05 11:20
S Sansanwal18-May-05 11:20 
GeneralFix formatting Pin
leppie19-Apr-05 19:28
leppie19-Apr-05 19:28 

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.